Assertion Failure in UICollectionViewData validate

2020-06-07 06:42发布

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 {
}

8条回答
冷血范
2楼-- · 2020-06-07 07:31

in iOS 10 you must disable the prefetchingEnabled:

// Swift
if #available(iOS 10, *) { 
    collectionView.prefetchingEnabled = false 
}

//Obj C
if ([self.collectionView respondsToSelector:@selector(setPrefetchingEnabled:)]) {
    self.collectionView.prefetchingEnabled = false;
}
查看更多
我欲成王,谁敢阻挡
3楼-- · 2020-06-07 07:35

You should implement invalidateLayout in your layout class and remove all kinds of UICollectionViewLayoutAttributes items in your config.

- (void)invalidateLayout{
  [super invalidateLayout];
  [self.itemsAttributes removeAllObjects];
}

For a better way to implement invalidateLayoutWithContext, see more about UICollectionViewLayoutInvalidationContext.

From Apple Developer documentation:

When implementing a custom layout, you can override this method and use it to process information provided by a custom invalidation context. You are not required to provide a custom invalidation context but might do so if you are able to provide additional properties that can help optimize layout updates. If you override this method, you must call super at some point in your implementation.

查看更多
登录 后发表回答