gpt4 book ai didi

ios - 在以编程方式实例化的 TableView Controller 中注册一个单元格

转载 作者:搜寻专家 更新时间:2023-11-01 05:56:50 25 4
gpt4 key购买 nike

我收到这个错误:

'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier NavigationNodeCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'

我正在使用两个具有相同类“NavigationItemCell”和两个标识符“NavigationNodeCell”和“NavigationLinkCell”的单元格。

我创建了一个单元格,例如:

let cell: NavigationItemCell  = self.tableView.dequeueReusableCellWithIdentifier("NavigationNodeCell",     forIndexPath: indexPath) as! NavigationItemCell

这个问题在我之前就已经存在了,例如Assertion failure in dequeueReusableCellWithIdentifier:forIndexPath:

据我所知(例如 Sebastian Borggrewe 的回答者)在 Storyboard中注册一个 UITableViewCell 及其自定义类和标识符就足够了。

这正是我所做的,但我仍然收到此错误。我尝试使用两个不同的类,但没有解决错误。我也尝试用 nib 注册我的单元格,但我遇到了其他问题(标签为零)。

我发现这个问题可能会发生,因为我以编程方式实例化了一个新的 TableView Controller 。

var newController = MyTableViewController()

这是否意味着无论如何我都必须注册 nib?我必须注册同一个类(class)两次,这是一个问题吗?以及如何避免 nil 标签的问题?

最佳答案

您应该使用 Storyboard ID 创建 View Controller 的实例。将行 var newController = MyTableViewController() 替换为以下代码

let storyboard = UIStoryboard(name: "YOUR_STORYBOARD_NAME", bundle: nil)
let newController = storyboard.instantiateViewController(withIdentifier: "VIEWCONTROLLER_ID_FROM_STORYBOARD")

编辑:也可以使用:self.storyboard!.instantiateViewControllerWithIdentifier("VIEWCONTROLLER_ID_FROM_STORYBOARD")

如果 View Controller 是从 Storyboard实例化的,并且第二个 View Controller 在同一个 Storyboard中

关于ios - 在以编程方式实例化的 TableView Controller 中注册一个单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39214408/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com