源码

iOS开发之UICollectionViewDataSourcePrefetching

在iOS10中,苹果为UICollectionViewCell引入了Pre-Fetching预加载机制用于提升它的性能。主要引入了一个新的数据源协议UICollectionViewDataSourcePrefetching,包含两个方法:

@protocol UICollectionViewDataSourcePrefetching <NSObject>
@required
// 预加载数据
- (void)collectionView:(UICollectionView *)collectionView prefetchItemsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths NS_AVAILABLE_IOS(10_0);
@optional
// 取消提前加载数据
- (void)collectionView:(UICollectionView *)collectionView cancelPrefetchingForItemsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths  NS_AVAILABLE_IOS(10_0);
@end

网上搜了一大圈,讲述原理的(翻译文档)的文章很多,有干货的Demo很少,于是乎自己摸索了一下,写了一个简单的案例,在此记录并分享一下。

运行环境:Xcode 8.2.1 + iOS 10.2

核心步骤:

1、遵从 UICollectionViewDataSourcePrefetching 协议
2、实现 collectionView:prefetchItemsAtIndexPaths 方法和collectionView:cancelPrefetchItemsAtIndexPaths 方法(可选)
3、将第1步中遵从协议的类设置为 UICollectionView 的 prefetchDataSource 属性

实现

一、创建UICollectionViewFlowLayout

自己写一个类继承自UICollectionViewFlowLayout

@implementation MyCollectionViewFlowLayout

-(void)prepareLayout{

    self.minimumLineSpacing = 1;//垂直间距
    self.minimumInteritemSpacing = 0;//水平间距
    self.sectionInset = UIEdgeInsetsMake(0080);//分组间距

}
@end

二、用XIB定义一个

里面就一个UIImageView,然后拽线设置一个IBOutlet

(0)

本文由 投稿者 创作,文章地址:https://blog.isoyu.com/archives/ioskaifazhiuicollectionviewdatasourceprefetching.html
采用知识共享署名4.0 国际许可协议进行许可。除注明转载/出处外,均为本站原创或翻译,转载前请务必署名。最后编辑时间为:1 月 9, 2019 at 02:00 下午

热评文章

发表回复

[必填]

我是人?

提交后请等待三秒以免造成未提交成功和重复