【问题标题】:NSInternalInconsistencyException while deleting cell from collectionView从collectionView中删除单元格时出现NSInternalInconsistencyException
【发布时间】:2016-02-15 07:01:25
【问题描述】:

我想从 UICollectionView 中删除一个单元格。在删除单元格时,我得到了

*** 由于未捕获的异常“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 时,我正在附加一个虚拟单元 这是 numberOfItemsInSection 中 numberOfItems 的代码

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

}

我在 Google 和 Stackoverflow 上找到了许多解决方案,但没有找到完全适合我的解决方案。

【问题讨论】:

  • 单次删除不需要performBatchUpdates。摆脱它,只保留块内的两行。
  • 你能展示你的numberOfItemsInSection方法吗?
  • 查看我更新的问题。 @Paulw11
  • 所以该代码解释了您的问题。数组中有 5 项,所以 numberOfItemsInSection 返回 5,然后删除 1,所以现在有 4,但 numberOfItemsInSection 仍然返回 5。这正是您的异常所说的。为什么会有那个 if 语句?
  • @Paulw11 :你是对的。正好 5 会引发问题。

标签: ios objective-c cocoa-touch uicollectionview


【解决方案1】:

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) {

}];

您也可以不使用 performBatchUpdates

直接尝试删除 1 个单元格
    [self.imgArray removeObjectAtIndex:indexForDelete];

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

    [self.collectionView reloadData];

希望,这会对您有所帮助。
谢谢

【讨论】:

  • 虽然这可以解决问题,但我建议您添加一些评论,解释为什么需要更新 performBatchUpdates 中的数组
  • @sohanvanani 你是如何管理 'cellForItemAtIndexPath' 方法的?
  • if([indexPath row] == self.imgArray.count) { cell.imageView.image = [UIImage imageNamed:@"add_Gallary"]; } else { //来自 imgArray 的单元格项目; }
【解决方案2】:

我认为您可能需要将更新分为两个阶段以避免 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;
            }
        }


    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-29
    • 1970-01-01
    • 2011-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多