从 UITableView 中删除最后一行 - NSInternalInconsistencyError 导致崩溃 [英] Deleting last row from UITableView - Crash with NSInternalInconsistencyError
问题描述
我尝试了所有可搜索的解决方案都无济于事.我目前的删除代码如下:
I've tried all searchable solutions to no avail. My current deletion code is as follows:
[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];
如您所见,在删除部分/行之前,我正在 _conversations 中从我的数据源中正确删除 - 如果它是最后一个部分,我也会删除该部分.
As you can see, I am properly deleting from my datasource in _conversations before deleting the section/row - and I am also deleting the section if it's the last one.
我仍然遇到崩溃:
由于未捕获的异常NSInternalInconsistencyException"而终止应用,原因:UITableView 内部错误:无法生成具有旧部分计数的新部分映射:1 和新部分计数:0"
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView internal bug: unable to generate a new section map with old section count: 1 and new section count: 0'
我没有想法,除非我做了一个非常hacky的解决方案,在我的实际内容上方插入一个不可见的部分,我真的不想这样做.非常感谢任何帮助.
I'm out of ideas, unless I do a really hacky solution where I insert an invisible section above my actual content, which I really don't want to do. Any help is greatly appreciated.
这是我的 numberOfSectionsInTableView 的样子:
Here is what my numberOfSectionsInTableView looks like:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
非常奇怪的更新:我试图通过在删除旧部分时插入一个空白部分来解决这个问题.但是由于某些无法解释的原因,我仍然收到错误消息.该错误坚持认为 tableview 的新部分计数为 0,即使我已将代码更改为以下内容:
Really weird update: I tried to do a hacky way of getting around this by inserting a blank section as I deleted the old one. But for some unexplainable reason I am STILL getting the error. The error insists that the tableview has a new section count of 0 even though I've changed my code to the following:
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];
}
完全不知道为什么会发生这种情况......
Completely clueless as to why this is happening at this point...
堆栈跟踪:
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;
}
}
推荐答案
这最终是另一个使用 queryController 的视图控制器的问题.通过确保正确删除行来修复该视图控制器后,错误消失了.
This ultimately was an issue with another view controller which used a queryController. After fixing that view controller by making sure rows were properly deleted, the error was gone.
这篇关于从 UITableView 中删除最后一行 - NSInternalInconsistencyError 导致崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!