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看到我的回答,摆出相同。 –