2017-05-27 50 views
0

我想重用我的UITableViewCells。目前我的應用工作正常,在tableView中顯示內容。然而,當我嘗試和實現 - 重用UITableViewCells,我的應用程序崩潰。我已經記下了下面的錯誤。請有人建議嗎?重用UITableViewCell

// 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 

回答

1

如果要重複使用cellForRowAt中的單元格,則需要首先將單元格註冊到tableView並使用reuseIdentifier。所以請註冊UITableViewCell類到tableView

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

ü在使用前寄存器單元到你的類cellForRow 就像你viewDidLoad方法是

[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"YourReuseIdentifier"]; 

0

如果你已經添加的UITableViewCell子類時,那麼你應該註冊筆尖,而不是類創建的tableview細胞筆尖。只要做,

tableView.register(UINib.init(nibName: "nibname", bundle: nil), forCellReuseIdentifier: "identifier")