UICollectionView项删除引发异常(UICollectionView item deletion raises exception)

在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'

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

Inside of UICollectionViewController this is deletion code.

- (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; }

Now when I delete items always from the end, it works just fine. However, when I delete some from the beginning, some from the middle, at some point I got an exception when I try to delete the last element:

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

I am not sure how to debug this...

最满意答案

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

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

I believe that the problem is the same as the one that arises when you delete an item from a UITableView without updating the datasource, thus causing the exception. Try the following:

[self.datasource removeObjectAtIndex:iPath.row]; //... delete the row from the collectionView.UICollectionView项删除引发异常(UICollectionView item deletion raises exception)

在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'

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

Inside of UICollectionViewController this is deletion code.

- (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; }

Now when I delete items always from the end, it works just fine. However, when I delete some from the beginning, some from the middle, at some point I got an exception when I try to delete the last element:

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

I am not sure how to debug this...

最满意答案

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

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

I believe that the problem is the same as the one that arises when you delete an item from a UITableView without updating the datasource, thus causing the exception. Try the following:

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