Assertion Failure in UICollectionViewData
validateLayoutInRect
on iOS7.
I am trying to delete all UICollectionView
items, one by one, using a for
loop; I posted my code below. I delete the UICollectionView
items using deleteItemsAtIndexPaths
. It's working perfectly on iOS6, but crashes in iOS7 with this exception:
Assertion Failure in UICollectionViewData validateLayoutInRect
I delete the object from collectionArray
then self.collectionView
, one by one, using indexPath
. When I delete the 4th object its raises Assertion failure
on iOS7. Here I am using performBatchUpdates
.
Please help me get the proper result in iOS7. Share proper code. Thanks in advance.
try {
for (int i=count-1; i>=0; i--) {
[self.collectionView performBatchUpdates:^(void){
[collectionArray removeObjectAtIndex:i]; // First delete the item from you model
[self.collectionView deleteItemsAtIndexPaths:@[[NSIndexPath indexPathForRow:i inSection:0]]];
} completion:nil];
[self.collectionView reloadData];
}
}
@catch (NSException *exception) {
}
@finally {
}
In my case following delegate was missing:
You should implement invalidateLayout in your
layoutClass
and remove all kinds ofUICollectionViewLayoutAttributes
items in your config.try to call
[yourCollectionView.collectionViewLayout invalidateLayout];
It looks like you probably want to do this:
So that all the updates are performed together. Otherwise you will end up trying to perform several lots of batch updates on top of each other.
I actually got this crash one time not because I was returning zero for a number of sections or items in a section but because I was reusing a flow layout like this for more than 1 collection view:
Instead if I create a new flow layout for each UICollectionView I avoid this crash. Hopefully that might help someone