今天遇到collectionView 刷新的時(shí)候崩潰的問(wèn)題,以此來(lái)記錄一下解決方法匹层。
這種崩潰有兩種報(bào)錯(cuò),分別是*** Assertion failure in -[UICollectionViewData validateLayoutInRect:] 和 [UICollectionView received layout attributes for a cell with an index path that does not exist]
產(chǎn)生這種崩潰的根源是collectionView reloadData時(shí)锌蓄,數(shù)據(jù)源已經(jīng)變了升筏,但是cell的layout attributes還沒(méi)有更新,也就是collectionViewLayout出了故障
解決方案有一下幾種
1.reload之前調(diào)整布局(對(duì)我有效)
[self setNeedsLayout];
2.(對(duì)我無(wú)效)
-(NSInteger) numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
[myCollectionView.collectionViewLayout invalidateLayout];
}
3.關(guān)閉iOS 10的新特性UICollectionViewCell的Pre-Fetching預(yù)加載(對(duì)我無(wú)效)
// Swift
if #available(iOS 10, *) {
collectionView.prefetchingEnabled = false
}
//Objective-C
if ([self.collectionView respondsToSelector:@selector(setPrefetchingEnabled:)]) {
self.collectionView.prefetchingEnabled = false;
}