从UITableView删除最后一行 – 崩溃与NSInternalInconsistencyError

我尝试了所有可search的解决scheme无济于事。 我目前的删除代码如下:

[self.conversationsTableView beginUpdates]; [_conversations removeObjectAtIndex:[indexPath row]]; if ([self.conversationsTableView numberOfRowsInSection:indexPath.section] == 1) { [self.conversationsTableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationAutomatic]; } else { [self.conversationsTableView deleteRowsAtIndexPaths:@[ indexPath ] withRowAnimation:UITableViewRowAnimationAutomatic]; } [self.conversationsTableView endUpdates]; 

正如你所看到的,在删除section / row之前,我在_conversations中正确删除了我的数据源,而且如果它是最后一个,我也删除了这个section。

我仍然崩溃了:

终止应用程序由于未捕获的exception'NSInternalInconsistencyException',原因:'UITableView内部错误:无法生成一个新的部分映射旧节数:1和新的节数:0'

我没有想法,除非我做了一个真正的黑客解决scheme,我在我的实际内容之上插入了一个看不见的部分,而我真的不想这样做。 任何帮助是极大的赞赏。

这是我的numberOfSectionsInTableView看起来像:

 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } 

真的很奇怪的更新 :我试图通过插入一个空白部分,因为我删除旧的一个绕过这个哈克的方式。 但是由于一些无法解释的原因,我仍然得到错误。 该错误坚持,即使我已经将我的代码更改为以下内容,tableview有一个新的部分计数为0:

 if ([self.conversationsTableView numberOfRowsInSection:indexPath.row] == 1) { [self.conversationsTableView deleteSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationAutomatic]; [self.conversationsTableView insertSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone]; } else { [self.conversationsTableView deleteRowsAtIndexPaths:@[ indexPath ] withRowAnimation:UITableViewRowAnimationAutomatic]; } 

完全不知道为什么这是在这一点上发生…

堆栈跟踪:

 NSInternalInconsistencyException', reason: 'UITableView internal bug: unable to generate a new section map with old section count: 1 and new section count: 0' *** First throw call stack: ( 0 CoreFoundation 0x000000010b6bee65 __exceptionPreprocess + 165 1 libobjc.A.dylib 0x000000010b137deb objc_exception_throw + 48 2 CoreFoundation 0x000000010b6becca +[NSException raise:format:arguments:] + 106 3 Foundation 0x000000010ad844de -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 198 4 UIKit 0x0000000109bef679 -[_UITableViewUpdateSupport(Private) _computeSectionUpdates] + 2858 5 UIKit 0x0000000109c00059 -[_UITableViewUpdateSupport initWithTableView:updateItems:oldRowData:newRowData:oldRowRange:newRowRange:context:] + 435 6 UIKit 0x00000001098cf912 -[UITableView _endCellAnimationsWithContext:] + 12936 7 WontBuyIOS 0x000000010819dea8 -[WOBMessagesViewController queryControllerDidChangeContent:] + 88 8 LayerKit 0x0000000108db4ca6 __65-[LYRQueryController updateWithObjectIdentifiers:changedObjects:]_block_invoke154 + 74 9 libdispatch.dylib 0x000000010c05249b _dispatch_client_callout + 8 10 libdispatch.dylib 0x000000010c03bc3c _dispatch_barrier_sync_f_slow_invoke + 284 11 libdispatch.dylib 0x000000010c05249b _dispatch_client_callout + 8 12 libdispatch.dylib 0x000000010c03a2af _dispatch_main_queue_callback_4CF + 1738 13 CoreFoundation 0x000000010b61ed09 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9 14 CoreFoundation 0x000000010b5e02c9 __CFRunLoopRun + 2073 15 CoreFoundation 0x000000010b5df828 CFRunLoopRunSpecific + 488 16 GraphicsServices 0x000000010e33cad2 GSEventRunModal + 161 17 UIKit 0x00000001097a4610 UIApplicationMain + 171 18 WontBuyIOS 0x00000001081a6bcf main + 111 19 libdyld.dylib 0x000000010c08692d start + 1 20 ??? 0x0000000000000001 0x0 + 1 ) 

插入/删除代码:

 - (void)queryController:(LYRQueryController *)controller didChangeObject:(id)object atIndexPath:(NSIndexPath *)indexPath forChangeType:(LYRQueryControllerChangeType)type newIndexPath:(NSIndexPath *)newIndexPath { switch (type) { case LYRQueryControllerChangeTypeInsert: { [_conversations addObject:object]; [self.conversationsTableView insertRowsAtIndexPaths:@[ newIndexPath ] withRowAnimation:UITableViewRowAnimationAutomatic]; } break; case LYRQueryControllerChangeTypeDelete: { [_conversations removeObjectAtIndex:indexPath.row]; if ([self.conversationsTableView numberOfRowsInSection:indexPath.row] == 1) { [self.conversationsTableView deleteSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationAutomatic]; [self.conversationsTableView insertSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone]; } else { [self.conversationsTableView deleteRowsAtIndexPaths:@[ indexPath ] withRowAnimation:UITableViewRowAnimationAutomatic]; } break; } default: break; } } 

numberOfRowsInSection调用_conversations.count 。 删除数据源数组中的项目后,它返回一个错误的值。 比较0而不是1

单次插入/删除/移动操作不需要beginUpdates / endUpdates

我的事情@rmaddy是正确的,你应该更新numberOfSections数据源,并返回1个较less的部分。 你有没有尝试调用[tableView reloadData]

看起来像打电话给numberOfRowsInSection:有副作用。 在if条件中使用_conversations.count 。 这应该解决你的第一个原始问题。

你正在调用[self.conversationsTableView numberOfRowsInSection:indexPath.row]并传入indexPath.row。 这是不正确的,应该是[indexPath section]

这最终是另一个使用queryController的视图控制器的问题。 通过确保行被正确删除来修复视图控制器后,错误消失了。