ios - 作为自定义 TableView 单元格的 Xib 文件崩溃

标签 ios iphone swift xcode uitableview

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

* Assertion failure in -[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] * 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' *** First throw call stack: ( 0 CoreFoundation 0x00000001035f6b0b exceptionPreprocess + 171 1 libobjc.A.dylib 0x000000010305b141 objc_exception_throw + 48 2 CoreFoundation 0x00000001035facf2 +[NSException raise:format:arguments:] + 98 3 Foundation 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 QuartzCore 0x000000010241d526 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 370 15 QuartzCore 0x000000010241d3a0 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24 16 QuartzCore 0x00000001023ace92 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 294 17 QuartzCore 0x00000001023d9130 _ZN2CA11Transaction6commitEv + 468 18 QuartzCore 0x00000001023d9b37 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 115 19 CoreFoundation 0x000000010359c717 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION + 23 20 CoreFoundation 0x000000010359c687 __CFRunLoopDoObservers + 391 21 CoreFoundation 0x0000000103581720 __CFRunLoopRun + 1200 22 CoreFoundation 0x0000000103581016 CFRunLoopRunSpecific + 406 23 GraphicsServices 0x00000001091e1a24 GSEventRunModal + 62 24 UIKit 0x0000000104191134 UIApplicationMain + 159 25 iCommander 0x0000000101e13ea7 main + 55 26 libdyld.dylib 0x000000010743e65d start + 1 27 ??? 0x0000000000000001 0x0 + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException

请帮忙。提前致谢。

最佳答案

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

n Swift 3

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

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

希望对您有所帮助。

关于ios - 作为自定义 TableView 单元格的 Xib 文件崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47632367/

相关文章:

android - 识别浏览器移动版本的最佳方法

iphone - 如何获取 UIImagePickerControllerReferenceURL 相机

ios - 线程 1 : Signal sigabrt. 不知道如何解决这个问题

ios - 在 swift 中对动态键和动态对象使用 Codable

ios - 异步下载后在可见的 UITableviewCell 中设置图像

ios - 为UIImage创建一个彩色的角

ios - 检测是否在后台/前台调用方法

ios - 打开具有特定地址 iOS 7 的 map

ios - 可定制的曲线图,可以显示不同点的图像

ios - 为什么没有在我的 UITabBarItem 上调用 didSelectItem?