import UIKit @main class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. let window = UIWindow(frame: UIScreen.main.bounds) self.window = window window.makeKeyAndVisible() window.rootViewController = UINavigationController(rootViewController: MainTableViewController()) return true } }
下から四行目のMainTableViewControllerに
'MainTableViewController' cannot be constructed because it has no accessible initializersというエラーが出てきて消えません。
解決方法を教えて欲しいです
MainTableViewController のソースはどうなってますか?
import UIKit
class Player {
let name: String
let image: UIImage
init(name: String, image: UIImage) {
self.name = name
self.image = image
}
}
class MainTableViewController: UITableViewController {
private let cellId = "cellId"
private let players = [
["山田","小林","田中","佐藤","卯類","竹富ダンテ"],
["山田","小林","田中","佐藤","卯類","竹富ダンテ"],
["山田","小林","田中","佐藤","卯類","竹富ダンテ"]
]
private let player = [
Player
]
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
tableView.register(MainTableViewCell.self, forCellReuseIdentifier: cellId)
setupNavigationBar()
}
private func setupNavigationBar() {
navigationItem.title = "NBA Player"
navigationItem.largeTitleDisplayMode = .always
navigationController?.navigationBar.prefersLargeTitles = true
//navigationの背景色を変更
let appearance = UINavigationBarAppearance()
appearance.configureWithDefaultBackground()
appearance.backgroundColor = UIColor(white: 0.9, alpha: 1)
navigationController?.navigationBar.scrollEdgeAppearance = appearance
navigationController?.navigationBar.standardAppearance = appearance
}
}
//MARK - tableViewの設定
extension MainTableViewController {
override func numberOfSections(in tableView: UITableView) -> Int {
return players.count
}
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 50
}
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let label = UILabel()
label.text = "Aチーム"
label.textAlignment = .center
label.backgroundColor = .darkGray
label.textColor = .white
switch section {
case 0:
label.text = "Aチーム"
case 1:
label.text = "Bチーム"
case 2:
label.text = "Cチーム"
default:
break
}
return label
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 60
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return players[section].count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! MainTableViewCell
cell.nameLabel.text = players[indexPath.section][indexPath.row]
//cell.textLabel?.text = players[indexPath.section][indexPath.row]
return cell
}
}
private let player = [
Player
]
のところがあやしいですね…。
ありがとうございます。解決しました。
回答1件
あなたの回答
tips
プレビュー