gpt4 book ai didi

ios - 重用 UITableViewCell

转载 作者:行者123 更新时间:2023-11-29 00:21:07 28 4
gpt4 key购买 nike

我正在尝试重用我的 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com