Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The number of sections contained in the collection view after the update must be equal to before the update

I've a UICollectionView that contains 10 sections and each sections have 2 cells. I am trying to delete a cell but I am getting this:

The number of sections contained in the collection view after the update (2) must be equal to the number of sections contained in the collection view before the update (2)

This is how I remove:

NSUInteger arrayLength = 1;
    NSUInteger integerArray[] = {0,0};
    NSIndexPath *aPath = [[NSIndexPath alloc] initWithIndexes:integerArray length:arrayLength];
    [self.collectionFavs deleteItemsAtIndexPaths:[NSArray arrayWithObject:aPath]];

I just want to delete a cell or section with the cells. What is my mistake?

like image 322
onivi Avatar asked Sep 12 '25 05:09

onivi


1 Answers

You should delete element from datasource array. F.e. if your method return 2 before deleting cell then after deleting it should return 1

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (notdeleted)
        return 2;
    else
        return 1;
}

In fact if you return [array count] in this method, then you should remove 1 object from array before calling -deleteItemsAtIndexPaths

NSUInteger integerArray[] = {0,0};
NSIndexPath *aPath = [[NSIndexPath alloc] initWithIndexes:integerArray length:arrayLength];
[array removeObjectAtIndex:0];
[self.collectionFavs deleteItemsAtIndexPaths:[NSArray arrayWithObject:aPath]];
like image 102
Josshad Avatar answered Sep 14 '25 19:09

Josshad