繁体   English   中英

更快的UICollectionView单元格

[英]Faster UICollectionView cells

关于将图像加载到UITableViews或UICollectionViews中,有很多答案。 但是,如果我的UICollectionView正在显示其他视图控制器的视图怎么办?

在我的UICollectionViewCell子类中说:

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {

        self.catViewController = [[CatViewController alloc] initWithNibName:nil bundle:nil];
        self.catViewController.view.frame = self.bounds;
        [self addSubview:self.catViewController.view];
    }
    return self;
}

在我的集合视图数据源中:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    MyCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:kSomeId forIndexPath:indexPath];

    cell.catViewController.data = self.data[indexPath.row]; //see below

    return cell;
}

catViewController具有data属性的设置器。 设置此属性后,猫会加载其图像以及该视图的其他一些相关图像。 那么,如何正确地重用MyCell单元,以使收集视图在每次创建(或重用)单元时不会卡住? 每个MyCell都占据了集合视图的整个宽度,该宽度水平滚动,因此,每当一个新单元格滚动到视图中时,集合视图就会停顿片刻。

对于高性能CollectionView单元,请在iOS10中将以下新内容与xcode8 配合使用

在ViewController中以以下方式实现协议“ UICollectionViewDataSourcePrefetching”

类ViewController:UIViewController,UICollectionViewDataSourcePrefetching {

在情节提要中(请参阅附图)或以编程方式将以下代表设置为您的收藏集视图

ViewController的 viewDidLoad方法中

collectionView.delegate =自我

collectionView.dataSource =自我

collectionView.prefetchDataSource =自我 在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM