UICollectionViewController
這是刪除代碼。UICollectionView項目刪除引發異常
- (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'
我不知道如何調試這...
我的猜測是,你沒有刪除從array.Please對象從陣列中刪除該對象並重新加載集合視圖 – vin
@vin看到我的回答,擺出相同。 –