【发布时间】:2016-02-06 12:35:52
【问题描述】:
我尝试了所有可搜索的解决方案均无济于事。我目前的删除代码如下:
[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 中正确地从我的数据源中删除 - 如果它是最后一个部分,我也会删除该部分。
我仍然遇到崩溃:
由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“UITableView 内部错误:无法生成旧节数:1 和新节数:0 的新节图”
我没有想法,除非我做了一个非常老套的解决方案,在我的实际内容上方插入一个不可见的部分,我真的不想这样做。非常感谢任何帮助。
这是我的 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;
}
}
【问题讨论】:
-
如果您要从表中删除一个部分,您需要更新数据源数据,以便
numberOfSections现在将返回一个更少的部分。
标签: ios objective-c uitableview