2016-02-15 92 views
0

我想从UICollectionView中删除单元格。虽然删除单元格,我得到NSInternalInconsistencyException从collectionView中删除单元格

***终止应用程序由于未捕获的异常“NSInternalInconsistencyException”,理由是:“无效的更新:在第0包含在现有的部分更新后的项目数项目数无效(5)必须等于更新之前该部分中包含的项目数(5),加上或减去从该部分插入或删除的项目数(插入0个,删除1个),加上或减去项目数移入或移出该部分(0移入,0移出)。'错误。

这里是我的代码:

[self.imgArray removeObjectAtIndex:indexForDelete]; 

[self.collectionView performBatchUpdates:^{ 

    NSIndexPath *indexPath =[NSIndexPath indexPathForItem:indexForDelete inSection:0]; 
    [self.collectionView deleteItemsAtIndexPaths:[NSArray arrayWithObject:indexPath]]; 
    } completion:^(BOOL finished) { 

}]; 

我追加一个虚拟池时,我的数组数不为5 这里是numberOfItems代码numberOfItemsInSection

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section 
{ 
    if(self.imgArray.count == 5) 
    { 
     return self.imgArray.count ; 
    } 
    else 
    { 
     return self.imgArray.count + 1; 
    } 

} 

我发现很多的解决方案谷歌和Stackoverflow,但没有发现帮助完整的我。

+0

你并不需要一个单一的删除'performBatchUpdates'。摆脱这一点,只是保持块内的两条线。 – Paulw11

+0

你可以显示你的'numberOfItemsInSection'方法吗? – Paulw11

+0

看到我更新的问题。 @ Paulw11 –

回答

0

performBatchUpdates主要用于执行多个单元格的操作。即删除,插入,移动。

试试这个,

[self.collectionObj performBatchUpdates:^{ 

    [self.imgArray removeObjectAtIndex:indexForDelete]; 

    NSIndexPath *indexPath =[NSIndexPath indexPathForItem:indexForDelete inSection:0]; 
    [self.collectionView deleteItemsAtIndexPaths:[NSArray arrayWithObject:indexPath]]; 

    [self.collectionView reloadData]; 

} completion:^(BOOL finished) { 

}]; 

您也可以直接尝试1个单元删除不使用performBatchUpdates

[self.imgArray removeObjectAtIndex:indexForDelete]; 

    NSIndexPath *indexPath =[NSIndexPath indexPathForItem:indexForDelete inSection:0]; 
    [self.collectionView deleteItemsAtIndexPaths:[NSArray arrayWithObject:indexPath]]; 

    [self.collectionView reloadData]; 

希望,这将帮助你。
谢谢

+0

虽然这会解决问题,但我建议您添加一些注释来解释为什么更新'performBatchUpdates'中的数组是必要的 – Paulw11

+0

尝试不起作用。 (Y) –

+0

@ Paulw11是的。图片= [UIImage imageNamed:@“add_Gallary”]; } else { // cell item from imgArray; } –

0

我想你可能需要将更新分成两个阶段以避免NSInternalInconsistencyException。

@property (assign, nonatomic) BOOL isMyCollectionViewUpdating; 

//... 

[self.imgArray removeObjectAtIndex:indexForDelete]; 

[self.collectionView performBatchUpdates:^{ 

    self.isMyCollectionViewUpdating = YES; 

    NSIndexPath *indexPath =[NSIndexPath indexPathForItem:indexForDelete inSection:0]; 
    [self.collectionView deleteItemsAtIndexPaths:[NSArray arrayWithObject:indexPath]]; 
} completion:^(BOOL finished) { 
     self.isMyCollectionViewUpdating = NO; 
     [self.collectionView reloadData]; 
}]; 

和集合视图numberOfItemInSection

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section 
    { 
     if (self.isMyCollectionViewUpdating) 
     { 
      return self.imgArray.count; 
     } else { 
      if(self.imgArray.count == 5) 
      { 
       return self.imgArray.count ; 
      } 
      else 
      { 
       return self.imgArray.count + 1; 
      } 
     } 


    }