tableView崩溃结束了超过16个项目

这很混乱。

我有一个UITableView ,更新和工作正常,直到它获得超过16项,然后崩溃时,试图endUpdates后调用insertRowsAtIndexPaths

被添加的NSIndexPath都是有效的。 -numberOfRowsInSection返回正确的数字。 它不会抛出与数据集相关的错误,而是与之崩溃

***终止应用程序由于未捕获的exception“NSInvalidArgumentException”,原因:'*** – [__ NSArrayM insertObject:atIndex:]:object can not be nil'

当在tableViewendUpdates时。

数据源都在那里, NSIndexPath很好。 代码工作正常0和16行之间,但是当我添加一个17日它崩溃。 另外,如果我用22个项目启动它,它工作正常,当我添加23日它崩溃…如果我调用重新加载数据,而不是更新和插入过程,它工作正常,所以这与数据本身没有任何关系,它不应该与我如何插入行,因为它工作通过16 …

我完全困惑。 这是我的更新方法。 它在任何时候都被主线程所调用。

 - (void)updateConversation:(NSNotification*)notification { NSDictionary *updateInfo = [notification userInfo]; //NSLog(@"Got update %@", updateInfo); if ([[updateInfo objectForKey:@"success"] integerValue] == YES) { [self updateConversationUI]; int addedStatementCount = [[updateInfo objectForKey:@"addedStatementCount"] intValue]; if (addedStatementCount > 0) { //[self.tableView reloadData]; [self.tableView beginUpdates]; int previousStatmentCount = [[updateInfo objectForKey:@"previousStatmentCount"] intValue]; NSLog(@"owner %i, Was %i, now %i, change of %i", self.owner, previousStatmentCount, (int)self.conversation.statements.count, addedStatementCount); NSMutableArray *rowPaths = [[NSMutableArray alloc] init]; for (int i = previousStatmentCount; i < previousStatmentCount + addedStatementCount; i++) { NSIndexPath *path = [NSIndexPath indexPathForRow:i inSection:0]; [rowPaths addObject:path]; } [self.tableView insertRowsAtIndexPaths:rowPaths withRowAnimation:UITableViewRowAnimationBottom]; [self.tableView endUpdates]; [self.tableView scrollToRowAtIndexPath:[rowPaths lastObject] atScrollPosition:UITableViewScrollPositionBottom animated:YES]; } } } 

其余的崩溃过去[self.tableView endUpdates]是UITableView

 *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil' *** First throw call stack: ( 0 CoreFoundation 0x000000010189b795 __exceptionPreprocess + 165 1 libobjc.A.dylib 0x00000001015fe991 objc_exception_throw + 43 2 CoreFoundation 0x0000000101852564 -[__NSArrayM insertObject:atIndex:] + 820 3 UIKit 0x0000000100317900 __46-[UITableView _updateWithItems:updateSupport:]_block_invoke691 + 173 4 UIKit 0x00000001002b5daf +[UIView(UIViewAnimationWithBlocks) _setupAnimationWithDuration:delay:view:options:factory:animations:start:animationStateGenerator:completion:] + 460 5 UIKit 0x00000001002b6004 +[UIView(UIViewAnimationWithBlocks) animateWithDuration:delay:options:animations:completion:] + 57 6 UIKit 0x00000001003174cb -[UITableView _updateWithItems:updateSupport:] + 2632 7 UIKit 0x0000000100312b18 -[UITableView _endCellAnimationsWithContext:] + 11615 8 Dev App 0x0000000100006036 -[ConversationViewController updateConversation:] + 998 9 CoreFoundation 0x00000001018f121c __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ + 12 10 CoreFoundation 0x000000010185370d _CFXNotificationPost + 2381 11 Dev App 0x00000001000055ac -[ConversationManager postNotification:] + 92 12 Foundation 0x0000000101204557 __NSThreadPerformPerform + 227 13 CoreFoundation 0x000000010182aec1 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17 14 CoreFoundation 0x000000010182a792 __CFRunLoopDoSources0 + 242 15 CoreFoundation 0x000000010184661f __CFRunLoopRun + 767 16 CoreFoundation 0x0000000101845f33 CFRunLoopRunSpecific + 467 17 GraphicsServices 0x00000001039a23a0 GSEventRunModal + 161 18 UIKit 0x0000000100261043 UIApplicationMain + 1010 19 Dev App 0x0000000100003613 main + 115 20 libdyld.dylib 0x0000000101f2a5fd start + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException 

这看起来像是在操作系统中的一个错误,堆栈会指示这是与animation块有关,但无论我设置animation,它都会发生,包括无。

并重新陈述。 这通过16个项目,然后插入项目过去导致崩溃。 它也被称为初始设置来加载数据,它会加载任何数量的项目,包括16以上。但是,当再次调用,纯粹是从16或更高的更新到更高的东西会崩溃。

我还没有遇到表视图最离奇的问题…

在设备和模拟器的iOS 7下,最新的Xcode作为参考。

看来,这个问题是由我scrollToRowAtIndexPath调用(即使它崩溃之前,它到达那里)引起的)与实施tableView:estimatedHeightForRowAtIndexPath结合:通过删除行高估计崩溃消失…仍然像一个错误在表格的animation系统给我。 幸运的是我不需要估计的行高,我忘记了我已经实现了它(试图发挥很好的iOS 7再次咬我)。

增加语句计数时放置战略断点 ,根据需要遍历循环添加行,并find它引起崩溃的语句 ,此时查看您的对象查找零值作为你正在尝试从数组中插入一个零对象(或不存在)的错误。

我个人build议你完全通过这个循环,同时保持path和以前的语句计数EXE。

  NSIndexPath *path = [NSIndexPath indexPathForRow:i inSection:0]; 

在我的情况下,tableView:heightForHeaderInSection:方法已经丢失,没有实现tableView:estimatedHeightForRowAtIndexPath方法,所以这也可以解决问题,如果你不使用tableView:estimatedHeightForRowAtIndexPath:

你的问题是你不更新tableView正在使用的数据源。 如果插入一行,则需要将适当的对象插入到tableView正在使用的数组中。

祝你好运!