ios - Nib 必须恰好包含一个顶级对象,该对象必须是 UITableViewCell 实例。在其他 UITableViews 中工作

标签 ios swift uitableview uikit

我有一个 UITableViewCell,我在单独的 .xib 文件中编写了它,用于几个不同的 UITableView。 TableViews都继承自一个类,该类注册了UINib以供使用,标识符为“SpecialCell”。

对于除一个 TableView 之外的所有 TableView,单元格都已出队并且工作正常。但是,对于其中一个,我收到此错误:

*** 断言失败 -[app.MyTableView _dequeueReusableViewOfType:withIdentifier:],/BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKitCore/UIKit-3899.13.13/UITableView.m:8546

*** 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“为标识符 (SpecialCell) 注册的 nib 无效 - nib 必须恰好包含一个顶级对象,该对象必须是 UITableViewCell 实例”

我使用与其他 TableView 中完全相同的代码使单元格出列,我无法弄清楚为什么这个问题只发生在一个 ViewController 中。

我看到了一些与同一消息相关的答案,但似乎没有一个适用于此。没有任何无关的顶级对象(并且由于单元格在其他 ViewController 中加载得非常好,所以我不认为这真的是根本问题)。

这是我用来使单元格出列的代码(在每个 ViewController 中都相同)。

guard let cell = tableView.dequeueReusableCell(withIdentifier: "SpecialCell", for: indexPath) as? SpecialCell else {
    print("can't load SpecialCell")
    return UITableViewCell()
}
return cell

(就其值(value)而言,该 else 子句中的 print 语句不会运行)。

如果有人对可能出现的问题有任何线索,我将不胜感激!

编辑:Picture of the SpecialCell nib as requested

最佳答案

在不同类之间使用相同的重用标识符不是一个好习惯。每次在 TableView 中使用 TableView 单元格时,都会使用唯一标识符注册它。这使 iOS 更容易识别您的手机。即使在文档中,他们也指定如果您重复使用相同的标识符,则旧注册将变得无效。

If you previously registered a class or nib file with the same reuse identifier, the nib you specify in the nib parameter replaces the old entry. You may specify nil for nib if you want to unregister the nib from the specified reuse identifier.

Docs

关于ios - Nib 必须恰好包含一个顶级对象,该对象必须是 UITableViewCell 实例。在其他 UITableViews 中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58062489/

相关文章:

ios - Xcode 7.3 更新后出现语法错误

ios - Google Maps sdk for iOS 中折线的轮廓

swift - 如何从 Spotify API 获取轨道 URI

SwiftUI - 如何获取单击按钮的坐标/位置

ios - 在单元格中的 ContentView 之外添加 View

ios - 如何在 ios swift 中重新加载 TableView 的可见单元格

ios - UITableview 向上滚动 UISectionHeader 的一半

iphone - subview 中的 TabBarController

ios - 根据单元格增加 TableView 高度

ios - 当点击单元格上的按钮时,在单元格中写入信息