我有一个 TableView ,当您点击其中一行时,它将实例化另一个 UITableViewController
。但是,我得到了这段代码:必须为标识符注册一个 nib 或一个类,或者在 Storyboard中连接一个原型(prototype)单元
这是 Storyboard:
这是我的代码
第一个 UITableViewController
class MainActivityViewController: UITableViewController {
let node = MenuStructure.structure.childenNode
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return node.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = node[indexPath.row].value
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let selectedNode = node[indexPath.row]
let subMenuViewController = SubMenuViewController(node: selectedNode)
navigationController?.pushViewController(subMenuViewController, animated: true)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//self.navigationItem.title = MenuStructure.structure.value
}
}
然后当你点击它的其中一行时,它会转到这个 UITableViewController
class SubMenuViewController: UITableViewController {
let node: Node
init(node: Node) {
self.node = node
super.init(nibName: nil, bundle: nil)
}
required init?(coder aDecoder: NSCoder) {
fatalError("required initializer not implemented")
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return node.childenNode.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
tableView.register(UITableViewCell, forCellReuseIdentifier: "cell")
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = node.childenNode[indexPath.row].value
return cell
}
override func viewDidLoad() {
}
}
我试过注册我的手机,但我不知道如何注册,也不知道该注册在哪里。
欢迎提出任何建议!谢谢!
更新代码
class SubMenuViewController: UITableViewController {
let node: Node
init(node: Node) {
self.node = node
super.init(nibName: nil, bundle: nil)
}
required init?(coder aDecoder: NSCoder) {
fatalError("required initializer not implemented")
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return node.childenNode.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = node.childenNode[indexPath.row].value
return cell
}
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(UITableViewController.self, forCellReuseIdentifier: "cell")
}
}
最佳答案
你应该在 viewDidLoad 方法中注册单元格,因为这个方法被调用一次所以它会被注册一次,然后 tableview 将通过出队重用这种类型的单元格
请像这样修改您的代码:
class SubMenuViewController: UITableViewController {
let node: Node
init(node: Node) {
self.node = node
super.init(nibName: nil, bundle: nil)
}
required init?(coder aDecoder: NSCoder) {
fatalError("required initializer not implemented")
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return node.childenNode.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = node.childenNode[indexPath.row].value
return cell
}
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
}
}
关于ios - 必须为标识符注册一个 nib 或一个类,或者在 Storyboard中连接一个原型(prototype)单元,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45625778/