首页 文章

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

提问于
浏览
0

我使用 Xib file 作为自定义 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对单元格进行出列 - 必须为标识符注册一个nib或类或在故事板中连接原型单元格' ***第一次抛出调用堆栈:(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:描述:] 193 4 UIKit 0x00000001042eae45 - [UITableView dequeueReusableCellWithIdentifier:forIndexPath:] 241 5 iCommander 0x0000000101e6b43f _TFC10iCommander25CommentsHistoryController9tableViewfTCS o11UITableView12cellForRowAtV10Foundation9IndexPath_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:以NSException类型的未捕获异常终止

请帮忙 . 提前致谢 .

3 回答

  • 0

    你忘了 register the XIB file . 您必须注册xib文件才能摆脱崩溃 .

    n Swift 3

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

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

    希望能帮助到你 .

  • 4

    将单元标识符设置为xib文件中的自定义单元格:
    setting cell identifier for reusing cell with that identifier

  • 0

    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
    
        }
    

相关问题