【问题标题】:Why does UITableView crash when I insert and delete at the same time?为什么我同时插入和删除时UITableView会崩溃?
【发布时间】:2025-12-18 02:30:02
【问题描述】:

我有以下代码:

@property (nonatomic, strong) NSMutableArray *rows; // pre-filled with 2 rows

// ...

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.rows.count;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSIndexPath *newRowIndexPath = [NSIndexPath indexPathForRow:indexPath.row+1 inSection:indexPath.section];
    MyRowRepresentation *newRowRepresentation = [MyRowRepresentation new];
    [self.rows insertObject:newRowRepresentation atIndex:newIndexPath.row];

    NSMutableIndexSet *indicesToRemove = [NSMutableIndexSet indexSet];
    NSMutableArray<NSIndexPath*> *indexPathsToRemove = [NSMutableArray array];

    for (int i = 0; i < self.row.count; i++)
    {
        MyRowRepresentation *mrr = self.rows[i];
        if (mrr != newRowRepresentation && [self meetsDeletionRequirements:mrr])
        {
            [indicesToRemove addIndex:i];
            [indexPathsToRemove addObject:[NSIndexPath indexPathForRow:i inSection:indexPath.section]];
        }
    }

    [tableView beginUpdates];
    [tableView insertRowsAtIndexPaths:@[newRowIndexPath] withRowAnimation:UITableViewRowAnimationTop];
    [self.rows removeObjectsAtIndexes:indicesToRemove];
    [tableView deleteRowsAtIndexPaths:indexPathsToRemove withRowAnimation:UITableViewRowAnimationTop];
    [tableView endUpdates]; // crash here
}

当我选择一行时,我得到了这个:

2015-12-14 20:27:17.761 Now[1685:1753114] ERROR CRASH #(null) attempt to insert row 3 into section 1, but there are only 3 rows in section 1 after the update
2015-12-14 20:27:17.828 Now[1685:1753114] ERROR Stack Trace: (
    0   CoreFoundation                      0x0000000185684248 <redacted> + 160
    1   libobjc.A.dylib                     0x00000001970a80e4 objc_exception_throw + 60
    2   CoreFoundation                      0x00000001856840ec <redacted> + 0
    3   Foundation                          0x0000000186538ed4 <redacted> + 112
    4   UIKit                               0x000000018a2e57ec <redacted> + 5256
    5   Now                                 0x0000000100092914 -[MyTableViewController tableView:didSelectRowAtIndexPath:] + 1234
    ...
)
2015-12-14 20:44:48.933 Now[1692:1754780] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempt to insert row 3 into section 1, but there are only 3 rows in section 1 after the update'

但我很困惑;以下步骤应该可行,不是吗?

  1. 我将新的行表示插入到我的跟踪数组中。
  2. 我开始更新表,这应该将其置于“暂停”状态。
  3. 我从我的跟踪数组中删除了我不想要的行表示,其中不包括新的。
  4. 我删除了对应于已删除行表示的表视图行。
  5. 我插入了对应于新行表示的表视图行。

为了使应用程序崩溃,我先选择原来的 2 行之一,然后再选择另一行。

我不明白导致崩溃的原因是什么?

【问题讨论】:

  • beginUpdates移动到方法的顶部
  • 调用deleteRowsAtIndexPaths必须在调用insertRowsAtIndexPaths之前。
  • @Paulw11 无需移动beginUpdates。它只需要在调用各种表格修改方法之前。
  • @rmaddy 实际上比这更微妙吗?第二次发生的操作(即插入或删除)的索引路径是否会受到首先发生的操作的影响,所以如果首先调用删除,那么插入点可能无效,反之亦然?
  • @Paulw11 正确。这在“iOS 表视图编程指南”中都有介绍。

标签: ios objective-c uitableview cocoa-touch


【解决方案1】:

我无法回答为什么,但我确实通过使用单个 reload 调用而不是多个 insert/delete 调用解决了这个问题:

@property (nonatomic, strong) NSMutableArray *rows; // pre-filled with 2 rows

// ...

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.rows.count;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView beginUpdates];
    NSIndexPath *newRowIndexPath = [NSIndexPath indexPathForRow:indexPath.row+1 inSection:indexPath.section];
    MyRowRepresentation *newRowRepresentation = [MyRowRepresentation new];
    [self.rows insertObject:newRowRepresentation atIndex:newIndexPath.row];

    NSMutableIndexSet *indicesToRemove = [NSMutableIndexSet indexSet];
    NSMutableArray<NSIndexPath*> *indexPathsToRemove = [NSMutableArray array];

    for (int i = 0; i < self.row.count; i++)
    {
        MyRowRepresentation *mrr = self.rows[i];
        if (mrr != newRowRepresentation && [self meetsDeletionRequirements:mrr])
        {
            [indicesToRemove addIndex:i];
            [indexPathsToRemove addObject:[NSIndexPath indexPathForRow:i inSection:indexPath.section]];
        }
    }

    [tableView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationAutomatic];
    [tableView endUpdates];
}

【讨论】: