I'm stucked with a strange crash and trying to fix it all day long. I have a custom UICollectionViewLayout that basically adds gravity and collision behavious to the cells.
The implementation works great! The problem happens when I try to delete one cell using: [self.collectionView performBatchUpdates:].
It gives me the following error:
2013-12-12 21:15:35.269 APPNAME[97890:70b] *** Assertion failure in -[UICollectionViewData validateLayoutInRect:], /SourceCache/UIKit_Sim/UIKit-2935.58/UICollectionViewData.m:357
2013-12-12 20:55:49.739 APPNAME[97438:70b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UICollectionView recieved layout attributes for a cell with an index path that does not exist: <NSIndexPath: 0x975d290> {length = 2, path = 0 - 4}'
My model is being handled correctly and I can see it removing the item from it!
The indexPaths of the item to delete is being passed correctly between objects. The only time the collectionView update doen't crash is when I delete the last cell on it, otherwise, the crash happens.
Here's the code I'm using to delete the cell.
- (void)removeItemAtIndexPath:(NSIndexPath *)itemToRemove completion:(void (^)(void))completion
{
UICollectionViewLayoutAttributes *attributes = [self.dynamicAnimator layoutAttributesForCellAtIndexPath:itemToRemove];
[self.gravityBehaviour removeItem:attributes];
[self.itemBehaviour removeItem:attributes];
[self.collisionBehaviour removeItem:attributes];
[self.collectionView performBatchUpdates:^{
[self.fetchedBeacons removeObjectAtIndex:itemToRemove.row];
[self.collectionView deleteItemsAtIndexPaths:@[itemToRemove]];
} completion:nil];
}
The CollectionView delegates that handles the cell attibutes are the basic ones below.
- (CGSize)collectionViewContentSize
{
return self.collectionView.bounds.size;
}
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
return [self.dynamicAnimator itemsInRect:rect];
}
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
return [self.dynamicAnimator layoutAttributesForCellAtIndexPath:indexPath];
}
Things I already tried with no success: - Invalidating the layout - Reloading the data - Removing the behaviours from the UIDynamicAnimator and adding them again after the update
Any insights?
A source code with the problem is available on this repository. Please check it out. Code Repository
Best. George.