添加UITableView时出错(I get error when I add a UITableView)

编程入门 行业动态 更新时间:2024-10-26 14:33:37
添加UITableView时出错(I get error when I add a UITableView)

我有一个static UITableView ,在其中一个cells ,我有一个dynamic UITableView 。 这是我使用的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell; if (self.tableView == tableView) { cell = [tableView cellForRowAtIndexPath:indexPath]; } else { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath]; [cell.textLabel setText:self.tagsArray [indexPath.row]]; } return cell; }

当我运行该应用程序时,它崩溃并出现以下错误:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:' *** First throw call stack: ( 0 CoreFoundation 0x0000000106efbc65 __exceptionPreprocess + 165 1 libobjc.A.dylib 0x0000000106b94bb7 objc_exception_throw + 45 2 CoreFoundation 0x0000000106efbaca +[NSException raise:format:arguments:] + 106 3 Foundation 0x00000001067a998f -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 195 4 UIKit 0x000000010762fa83 -[UITableView _configureCellForDisplay:forIndexPath:] + 128 5 UIKit 0x0000000107637a41 -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] + 533 6 UIKit 0x0000000107616248 -[UITableView _updateVisibleCellsNow:isRecursive:] + 2853 7 UIKit 0x000000010762c8a9 -[UITableView layoutSubviews] + 210 8 UIKit 0x00000001075b6a2b -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 536 9 QuartzCore 0x0000000106588ec2 -[CALayer layoutSublayers] + 146 10 QuartzCore 0x000000010657d6d6 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380 11 QuartzCore 0x000000010657d546 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24 12 QuartzCore 0x00000001064e9886 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 242 13 QuartzCore 0x00000001064eaa3a _ZN2CA11Transaction6commitEv + 462 14 QuartzCore 0x00000001064eb0eb _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 89 15 CoreFoundation 0x0000000106e2eca7 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23 16 CoreFoundation 0x0000000106e2ec00 __CFRunLoopDoObservers + 368 17 CoreFoundation 0x0000000106e24a33 __CFRunLoopRun + 1123 18 CoreFoundation 0x0000000106e24366 CFRunLoopRunSpecific + 470 19 GraphicsServices 0x000000010ad61a3e GSEventRunModal + 161 20 UIKit 0x0000000107536900 UIApplicationMain + 1282 21 myBudget 0x000000010611fe5f main + 111 22 libdyld.dylib 0x0000000108952145 start + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException

为什么我会收到此错误,我该怎么做才能修复它?

I have a static UITableView, and in one of the cells, I have a dynamic UITableView. Here is the code I used:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell; if (self.tableView == tableView) { cell = [tableView cellForRowAtIndexPath:indexPath]; } else { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath]; [cell.textLabel setText:self.tagsArray [indexPath.row]]; } return cell; }

When I run the app, it crashes with the following error:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:' *** First throw call stack: ( 0 CoreFoundation 0x0000000106efbc65 __exceptionPreprocess + 165 1 libobjc.A.dylib 0x0000000106b94bb7 objc_exception_throw + 45 2 CoreFoundation 0x0000000106efbaca +[NSException raise:format:arguments:] + 106 3 Foundation 0x00000001067a998f -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 195 4 UIKit 0x000000010762fa83 -[UITableView _configureCellForDisplay:forIndexPath:] + 128 5 UIKit 0x0000000107637a41 -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] + 533 6 UIKit 0x0000000107616248 -[UITableView _updateVisibleCellsNow:isRecursive:] + 2853 7 UIKit 0x000000010762c8a9 -[UITableView layoutSubviews] + 210 8 UIKit 0x00000001075b6a2b -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 536 9 QuartzCore 0x0000000106588ec2 -[CALayer layoutSublayers] + 146 10 QuartzCore 0x000000010657d6d6 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380 11 QuartzCore 0x000000010657d546 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24 12 QuartzCore 0x00000001064e9886 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 242 13 QuartzCore 0x00000001064eaa3a _ZN2CA11Transaction6commitEv + 462 14 QuartzCore 0x00000001064eb0eb _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 89 15 CoreFoundation 0x0000000106e2eca7 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23 16 CoreFoundation 0x0000000106e2ec00 __CFRunLoopDoObservers + 368 17 CoreFoundation 0x0000000106e24a33 __CFRunLoopRun + 1123 18 CoreFoundation 0x0000000106e24366 CFRunLoopRunSpecific + 470 19 GraphicsServices 0x000000010ad61a3e GSEventRunModal + 161 20 UIKit 0x0000000107536900 UIApplicationMain + 1282 21 myBudget 0x000000010611fe5f main + 111 22 libdyld.dylib 0x0000000108952145 start + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException

Why do I get this error, and what can I do to fix it?

最满意答案

尝试使用以下代码

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *simpleTableIdentifier = @"SimpleTableItem"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier]; } cell.textLabel.text = [self.tagsArray objectAtIndex:indexPath.row]; return cell; }

Try using below code

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *simpleTableIdentifier = @"SimpleTableItem"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier]; } cell.textLabel.text = [self.tagsArray objectAtIndex:indexPath.row]; return cell; }

更多推荐

本文发布于:2023-08-07 06:04:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1462055.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:UITableView   error   add

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!