手机系统iOS11滑动UICollectionView崩溃的问题
2017-12-22 本文已影响252人
不会写代码的尬先生
今天同事遇到一个诡异的问题,在ios11版本的手机上滑动UICollectionView闪退,定位到错误信息-[UICollectionViewData validateLayoutInRect:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit/UIKit-3694.4.18/UICollectionViewData.m:447,
神勇的我当然大显身手 —— 一番Google(🐶)找到了类似问题。原来ios10的新特性UICollectionViewCell的Pre-Fetching预加载,ios10苹果官方为了UICollectionView和UITableViewCell性能的提升,新加了Pre-Fetching API。
除了原先的data source和delegate,新加了prefetchDataSource,协议中有两个方法:ColletionView prefetchItemsAt indexPaths和CollectionView cancelPrefetcingForItemsAt indexPaths
ColletionView prefetchItemsAt indexPaths。这个方法会在prefetchDataSource里面被调用,异步的预加载数据。indexPaths数组是有序的,就是接下来item接收数据的顺序,方便model异步处理数据。
CollectionView cancelPrefetcingForItemsAt indexPaths,是optional的。可以在滑动中取消或者降低提前加载数据的优先级。
我们的UICollectionView的头部会不停的计算高度,自定义了UICollectionViewFlowLayout,所以崩溃问题的暂时解决方案是关闭Fetching,
解决方案如下:
// Swift
if #available(iOS 10, *) {
collectionView.prefetchingEnabled = false
}
//Objective-C
if ([self.collectionView respondsToSelector:@selector(setPrefetchingEnabled:)]) {
self.collectionView.prefetchingEnabled = false;
}
以上。