ios - 重用 UITableViewCell

标签 ios swift3

我正在尝试重用我的 UITableViewCells。目前我的应用程序运行良好,在 tableView 中显示内容。然而,当我尝试实现 - 重用 UITableViewCells 时,我的应用程序崩溃了。我已记下下面的错误。请问有人可以建议吗?

 // This code works
 let cell = UITableViewCell.init(style: .subtitle, reuseIdentifier: "UITableViewCell")

 // However, When I replace it with the following my app crashes.
 let cell = self.tableView.dequeueReusableCell(withIdentifier: "UITableViewCell", for: indexPath)

控制台中的错误:

2017-05-27 12:35:40.623 lifesci-PubMed[86784:9245533] *** Assertion failure in -[UITableView dequeueReusableCellWithIdentifier:forIndexPath:],     /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-     3600.6.21/UITableView.m:6600
2017-05-27 12:35:40.639 lifesci-PubMed[86784:9245533] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier UITableViewCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
*** First throw call stack:
(
0   CoreFoundation                      0x000000010e56ed4b __exceptionPreprocess + 171
1   libobjc.A.dylib                     0x000000010dfd021e objc_exception_throw + 48
2   CoreFoundation                      0x000000010e572e42 +[NSException raise:format:arguments:] + 98
3   Foundation                          0x000000010db6566d -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 195
4   UIKit                               0x000000010eaed1e8 -[UITableView dequeueReusableCellWithIdentifier:forIndexPath:] + 259
5   lifesci-PubMed                      0x000000010d99ae04 _TFC14lifesci_PubMed27MySearchTableViewController9tableViewfTCSo11UITableView12cellForRowAtV10Foundation9IndexPath_CSo15UITableViewCell + 276
6   lifesci-PubMed                      0x000000010d99b297 _TToFC14lifesci_PubMed27MySearchTableViewController9tableViewfTCSo11UITableView12cellForRowAtV10Foundation9IndexPath_CSo15UITableViewCell + 87
7   UIKit                               0x000000010eb00584 -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] + 757
8   UIKit                               0x000000010eb007e2 -[UITableView _createPreparedCellForGlobalRow:willDisplay:] + 74
9   UIKit                               0x000000010ead42b0 -[UITableView _updateVisibleCellsNow:isRecursive:] + 3295
10  UIKit                               0x000000010eb09b64 -[UITableView _performWithCachedTraitCollection:] + 110
11  UIKit                               0x000000010eaf03be -[UITableView layoutSubviews] + 222
12  UIKit                               0x000000010ea57ab8 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1237
13  QuartzCore                          0x0000000113b03bf8 -[CALayer layoutSublayers] + 146
14  QuartzCore                          0x0000000113af7440 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366
15  QuartzCore                          0x0000000113af72be _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
16  QuartzCore                          0x0000000113a85318 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 280
17  QuartzCore                          0x0000000113ab23ff _ZN2CA11Transaction6commitEv + 475
18  QuartzCore                          0x0000000113ab2d6f _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 113
19  CoreFoundation                      0x000000010e513267 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
20  CoreFoundation                      0x000000010e5131d7 __CFRunLoopDoObservers + 391
21  CoreFoundation                      0x000000010e4f7f8e __CFRunLoopRun + 1198
22  CoreFoundation                      0x000000010e4f7884 CFRunLoopRunSpecific + 420
23  GraphicsServices                    0x0000000112bfda6f GSEventRunModal + 161
24  UIKit                               0x000000010e992c68 UIApplicationMain + 159
25  lifesci-PubMed                      0x000000010d9b579f main + 111
26  libdyld.dylib                       0x0000000111c6d68d start + 1
27  ???                                 0x0000000000000001 0x0 + 1
 )
 libc++abi.dylib: terminating with uncaught exception of type NSException

最佳答案

如果要在cellForRowAt 中重用单元格,则需要先使用reuseIdentifier 将单元格注册到tableView。因此,将 UITableViewCell 类注册到 tableView

override func viewDidLoad() {
    super.viewDidLoad()
    self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "UITableViewCell")
}

关于ios - 重用 UITableViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44216863/

相关文章:

iphone - 支持旧的 iOS 版本和设备

ios - swift 3.0 : "objc_setAssociatedObject" issue

ios - Swift 错误无法转换类型 '(AFHTTPRequestOperation?, AnyObject?) 的值 -> ()

sprite-kit - 错误 : "Property ' self. gameArea' 未在 super.init 调用中初始化”

ios - 属性(property)观察员及其行为

ios - 想要对两个不同的情况使用 UIViewController 流

ios - 忽略的属性和简单的 swift 属性

swift - 返回上一个 View Controller 时重新加载 Collection View

ios - 如何从 GPUImage 相机获取 UIImage?

ios - 将未完成的 iOS 应用程序提交到 Apple Store 以获得 Apple 的一些初步反馈是否有意义?