【问题标题】:Xib file as a Custom TableView Cell crashesXib 文件作为自定义 TableView 单元崩溃
【发布时间】:2018-05-17 20:38:00
【问题描述】:

我使用 Xib 文件 作为自定义 Tableview 单元格。但是每次我运行我的应用程序时,它都会崩溃。我收到了我在下面发布的崩溃日志

* -[UITableView dequeueReusableCellWithIdentifier:forIndexPath:] 中的断言失败,/BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3600.7.47/UITableView.m:6730 2017-12-04 17:23:16.070 iCommander[2489:135476] * 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无法使具有标识符 Cell 的单元格出列 - 必须注册一个 nib 或一个类用于标识符或连接故事板中的原型单元' *** 首先抛出调用堆栈: ( 0 核心基础 0x00000001035f6b0b 异常预处理 + 171 1 libobjc.A.dylib 0x000000010305b141 objc_exception_throw + 48 2 CoreFoundation 0x00000001035facf2 + [NSException raise:format:arguments:] + 98 3 基础 0x0000000102bf5536 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 193 4 UIKit 0x00000001042eae45-[UITableView dequeueReusableCellWithIdentifier:forIndexPath:] + 241 5 iCommander 0x0000000101e6b43f _TFC10iCommander25CommentsHistoryController9tableViewfTCSo11UITableView12cellForRowAtV10Foundation9IndexPath_CSo15UITableViewCell + 303 6 iCommander 0x0000000101e6ba07 _TToFC10iCommander25CommentsHistoryController9tableViewfTCSo11UITableView12cellForRowAtV10Foundation9IndexPath_CSo15UITableViewCell + 87 7 UIKit 0x00000001042fdab2-[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] + 750 8 UIKit 0x00000001042fdcf8-[UITableView _createPreparedCellForGlobalRow:willDisplay:] + 74 9 UIKit 0x00000001042d2639-[UITableView _updateVisibleCellsNow:isRecursive:] + 2845 10 UIKit 0x0000000104306ccc-[UITableView _performWithCachedTraitCollection:] + 111 11 UIKit 0x00000001042ede7a-[UITableView layoutSubviews] + 233 12 UIKit 0x000000010425455b-[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1268 13 QuartzCore 0x0000000102429904 -[CALayer layoutSublayers] + 146 14 石英核心 0x000000010241d526 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 370 15 石英核心 0x000000010241d3a0 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24 16 石英核心 0x00000001023ace92 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 294 17 石英核心 0x00000001023d9130 _ZN2CA11Transaction6commitEv + 468 18 石英核心 0x00000001023d9b37 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 115 19 核心基础 0x000000010359c717 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION + 23 20 核心基础 0x000000010359c687 __CFRunLoopDoObservers + 391 21 核心基础 0x0000000103581720 __CFRunLoopRun + 1200 22 核心基础 0x0000000103581016 CFRunLoopRunSpecific + 406 23 图形服务 0x00000001091e1a24 GSEventRunModal + 62 24 UIKit 0x0000000104191134 UIApplicationMain + 159 25 iCommander 0x0000000101e13ea7 主要 + 55 26 libdyld.dylib 0x000000010743e65d 开始 + 1 27 ??? 0x0000000000000001 0x0 + 1 ) libc++abi.dylib:以 NSException 类型的未捕获异常终止

请帮忙。提前致谢。

【问题讨论】:

  • 您阅读崩溃日志了吗? Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier Cell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
  • 知道了。谢谢您的帮助。问题解决了。

标签: ios iphone swift xcode uitableview


【解决方案1】:

您忘记注册 XIB 文件。您必须注册 xib 文件才能摆脱崩溃。

In 斯威夫特 3

 let xib = UINib(nibName: "YourCustomCell", bundle: nil)
 tableView.register(xib, forCellReuseIdentifier: "Cell")

您应该像这样注册自定义单元格。

希望对你有帮助。

【讨论】:

  • 谢谢伙计。解决了。无法投票给你。我目前没有资格投票。
【解决方案2】:

将单元格标识符设置为xib文件中的自定义单元格为:

【讨论】:

  • 我已经使用了标识符。我错过了一件事,那就是我没有注册单元格。顺便说一句,感谢您的帮助。问题解决了。
【解决方案3】:

IN Swift 3

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        let cell = Bundle.main.loadNibNamed("XibName", owner: self, options: nil)?.first as! XibName

 return cell

    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-20
    • 2012-09-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多