I'm getting an assertion while deleting a single UICollecitonViewCell
from UICollectionView
.
Precondition: I have a single cell (when I have two or more cells, the deletion works good).
Here is the code:
NSIndexPath *ip = [_photosCollectionView indexPathForCell:cell];
[_datasource removeItemAtIndex:ip.item];
[_photosCollectionView deleteItemsAtIndexPaths:@[ip]]; // the assertion is raised
Here is the assertion text:
NSInternalInconsistencyException: attempt to delete item 0 from section 0 which only contains 0 items before the update
Quite strange issue, because it works for 2, 3 or more cells, but when I delete a single cell, it fails.
Any ideas what's wrong, how to work-around this issue?
Build and Run the project and select the Edit Button. Select a few cells and press the Trash button to remove the items.
An object that manages an ordered collection of data items and presents them using customizable layouts.
Thanks to similar questions and answers on SO, found a solution to use performBatchUpdates
:
[_photosCollectionView performBatchUpdates:^ {
[_datasource removeItemAtIndex:ip.item];
[_photosCollectionView deleteItemsAtIndexPaths:@[ip]]; // no assertion now
} completion:nil];
Same solution with Swift 4.1
let indexPath = IndexPath(item: 0, section: 0)
self.collectionView.performBatchUpdates({
self.collectionView.deleteItems(at:[indexPath])
}, completion:nil)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With