当我同时插入和删除时,为什么UITableView会崩溃?

时间:2015-12-15 01:46:30

标签: ios objective-c uitableview cocoa-touch

我有以下代码:

@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. 我插入了对应于新行表示的表视图行。
  6. 要使应用程序崩溃,我选择原始的2行之一,然后选择另一行。

    我不明白是什么导致这种情况崩溃?

1 个答案:

答案 0 :(得分:0)

我无法回答为什么,但我确实通过使用单个重新加载调用而不是多个插入/删除调用来解决此问题:

@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];
}