簡體   English   中英

為什么同時插入和刪除UITableView會崩潰?

[英]Why does UITableView crash when I insert and delete at the same time?

我有以下代碼:

@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. 我插入對應於新行表示的表格視圖行。

要使應用程序崩潰,請選擇原始的兩行之一,然后選擇另一行。

我不明白是什么導致此崩潰?

我無法回答為什么 ,但是我確實通過使用單個重新加載調用而不是多個插入/刪除調用來解決了這個問題:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM