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

Noo*_*Dev 0 iphone xcode uitableview ios swift

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

* 断言失败 -[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 的单元出列 - 必须为标识符注册一个笔尖或类,或者连接故事板中的原型单元”* ** 首先抛出调用堆栈:(0 CoreFoundation 0x00000001035f6b0bexceptionPreprocess + 171 1 libobjc.A.dylib 0x000000010305b141 objc_exception_throw + 48 2的CoreFoundation 0x00000001035facf2 + [NSException提高:格式:参数:] + 98 3基础0x0000000102bf5536 - [NSAssertionHandler handleFailureInMethod:对象:文件:LINENUMBER:描述:] + 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] + 23305010ViewerLayout(UITableView layoutSubviews)[UITableView layoutSubviews][UITableView _performWithCachedTraitCollection:]] + 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_FUNCTION0x00000001023d9130 _ZN2CA11Transaction6commitEv + 468 18 QuartzCore 0x00000001023d9b37 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 115 19的CoreFoundation 0x000000010359c717 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION0x00000001023d9130 _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主+ 55 26 libdyld.dylib 0x000000010743e65d启动+ 1 27 ??? 0x0000000000000001 0x0 + 1 ) libc++abi.dylib: 以未捕获的 NSException 类型异常终止

请帮忙。提前致谢。

Ria*_*man 5

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

斯威夫特 3

 let xib = UINib(nibName: "YourCustomCell", bundle: nil)
 tableView.register(xib, forCellReuseIdentifier: "Cell")
Run Code Online (Sandbox Code Playgroud)

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

希望能帮助到你。