Reputation: 173
I am trying to reuse my UITableViewCells. At the moment my app works fine, displaying the content in tableView. However when I try and implement - reusing UITableViewCells, my app crashes. I have noted down the error below. Please can someone advise?
// 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)
The error in the console:
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
Upvotes: 0
Views: 94
Reputation: 270
if you have created tableview cell nib when adding UITableViewCell subclass then you should register nib instead of class. Just do,
tableView.register(UINib.init(nibName: "nibname", bundle: nil), forCellReuseIdentifier: "identifier")
Upvotes: 0
Reputation: 72460
If you want to reuse the cell in cellForRowAt
then you need to first register the cell to tableView
with reuseIdentifier
. So register the UITableViewCell
class to tableView
.
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "UITableViewCell")
}
Upvotes: 1
Reputation: 191
U have to register cell to your class before use in cellForRow like that
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"YourReuseIdentifier"];
in your viewDidLoad method!
Upvotes: 0