ios - 必须为标识符注册一个 nib 或一个类,或者在 Storyboard中连接一个原型(prototype)单元

标签 ios swift

我有一个 TableView ,当您点击其中一行时,它将实例化另一个 UITableViewController。但是,我得到了这段代码:必须为标识符注册一个 nib 或一个类,或者在 Storyboard中连接一个原型(prototype)单元

这是 Storyboard:

enter image description here

这是我的代码

第一个 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/

相关文章:

ios - 使用 Swift 自定义 TableView 的页眉和页脚

swift - 在 Swift 3 中将 Int 附加到数据

ios - 滚动 tableView 时复制数据

swift - 我如何使用 RxSwift 让我的程序快速响应

ios - 在 Facebook 上分享图片时添加默认句子

android - 如何在移动应用程序的 sails.js 中从以前的设备注销用户?

ios - 如何在 iOS 中设计类似 UITabBarItem 的 UIButton?

iphone - 构建成功但没有显示模拟器

objective-c - 使用异步函数和 Reactive-Cocoa 转换值数组

ios - 转换成json后如何json列表