Ios 正在从UITableView中删除最后一行-因NSInternalInconsistencyError而崩溃

Ios 正在从UITableView中删除最后一行-因NSInternalInconsistencyError而崩溃,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我尝试了所有可搜索的解决方案,但都没有结果。我当前的删除代码如下: [self.conversationsTableView beginUpdates]; [_conversations removeObjectAtIndex:[indexPath row]]; if ([self.conversationsTableView numberOfRowsInSection:indexPath.section] == 1) { [self.conversationsTableView del

我尝试了所有可搜索的解决方案,但都没有结果。我当前的删除代码如下:

[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];
正如您所看到的,在删除节/行之前,我在_对话中正确地从我的数据源中删除-如果是最后一个节,我也在删除该节

我还是会崩溃:

由于未捕获的异常“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;
    }
}

numberofrowsinssection
呼叫
\u对话。计数
。删除数据源数组中的项后,它返回错误的值。与
0
比较,而不是
1


beginUpdates/endUpdates
对于单个插入/删除/移动操作是不需要的。

I thing@rmaddy是正确的,您应该更新
numberOfSections
数据源,并少返回1个节。
您是否尝试过调用
[tableView reloadData]

调用
numberOfRowsInSection:
有副作用。使用
\u对话。在
条件下计数
。这将解决您的第一个原始问题。

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

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

如果要从表中删除节,则需要更新数据源数据,以便
numberOfSections
现在将少返回一个节。不幸的是,这没有修复它-您的逻辑是正确的,但出于某种原因,UITableView将缓存numberOfRowsInSection的值。所以我仍然需要与1进行比较。无论如何,对deleteSections的调用肯定会被断点调用以确认-它仍然会给我不一致的错误。我已经尝试过了-如果数据源中有对象,我让numberOfSections返回1,如果没有,则返回0。我不想使用重载数据,因为我想保留动画。将其设置为1后是否仍显示相同的错误您可以尝试此添加检查,以确保在删除节之前tableView中至少有2个节,如果只剩下1个节,则删除剩下的行,而不是删除该节。仅不过,请在我的tableView中使用一个部分。此外,无论我是否有一个节,如果我调用deleteSection或deleteRows,我都会得到相同的错误。如果您仅使用一个节,则在删除旧节之前,向表中添加一个新节,然后删除旧节