UICollectionView项删除引发异常

时间:2014-03-19 12:43:35

标签: ios uicollectionview

UICollectionViewController内,这是删除代码。

- (IBAction)tapRead:(id)sender
{
    if (self.editing)
    {
        BookCell *cell = (BookCell*)[[sender superview] superview];
        NSArray *arr = [NSArray arrayWithObjects:cell.ipath, nil];

        _totalCount--;
        NSLog(@"total: %d", _totalCount);

        [self.collectionView deleteItemsAtIndexPaths:arr];
    }
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _totalCount;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath; {
    BookCell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"BookCell" forIndexPath:indexPath];
    cell.ipath = indexPath;

    if (self.editing) {
        [cell engageEditMode];
    }
    else
    {
        [cell exitEditMode];
    }

    return cell;
}

现在,当我总是从最后删除项目时,它工作得很好。但是,当我从头开始删除一些时,从中间删除一些,当我尝试删除最后一个元素时,我得到了一个异常:

'NSInternalInconsistencyException', reason: 'attempt to delete item 24 from section 0 which only contains 20 items before the update'

我不知道如何调试这个......

1 个答案:

答案 0 :(得分:0)

我认为问题与在不更新数据源的情况下从UITableView中删除项目时出现的问题相同,从而导致异常。请尝试以下方法:

[self.datasource removeObjectAtIndex:iPath.row];
//... delete the row from the collectionView.