0
votes

I want to delete a cell from a UICollectionView. While deleting cell I get

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of items in section 0. The number of items contained in an existing section after the update (5) must be equal to the number of items contained in that section before the update (5), plus or minus the number of items inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of items moved into or out of that section (0 moved in, 0 moved out).' Error.

Here is my code:

[self.imgArray removeObjectAtIndex:indexForDelete];

[self.collectionView performBatchUpdates:^{

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

 }];

I am appending one dummy cell when my array count is not 5 Here is code for numberOfItems in numberOfItemsInSection

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

}

I found many solution on Google and Stackoverflow but nothing found help full for me.

2
You don't need the performBatchUpdates for a single delete. get rid of that and just keep the two lines that are inside the block. - Paulw11
Can you show your numberOfItemsInSection method? - Paulw11
see my updated question. @Paulw11 - sohan vanani
So that code explains your problem. You have 5 items in the array, so numberOfItemsInSection returns 5, then you delete 1, so now you have 4, but numberOfItemsInSection still returns 5. This is exactly what your exception says. Why do you have that if statement? - Paulw11
@Paulw11 : you are right. Exactly 5 will raise issue. - Manann Sseth

2 Answers

0
votes

performBatchUpdates mainly used to perform actions for multiple cells. i.e. delete, insert, move.

Try this,

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

}];

You can also try directly for 1 cell delete without using performBatchUpdates

    [self.imgArray removeObjectAtIndex:indexForDelete];

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

    [self.collectionView reloadData];

Hope, this will help you.
Thanks

0
votes

I think you may need to separate the update into two stages to avoid the 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];
}];

and for the collection view 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;
            }
        }


    }