【问题标题】:Faster UICollectionView cells更快的 UICollectionView 单元格
【发布时间】:2014-03-29 01:06:36
【问题描述】:

对于将图像加载到 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 有一个数据属性的设置器。设置此属性后,猫将加载它的图像以及该视图的其他一些相关图像。那么如何正确重用 MyCell 单元格,以便集合视图不会在每次创建(或重用)单元格时出现口吃?每个 MyCell 都占据了水平滚动的集合视图的整个宽度,因此每次新单元格滚动到视图中时,集合视图都会暂停片刻。

【问题讨论】:

  • 如果设置单元格花费太多时间,您可能需要同时填充虚拟单元格,并将图像加载到数据设置器的完成块中。这样,当单元格加载后,您会看到单元格在占位符上方淡入,但滚动可以保持流畅。
  • 我认为您需要发布在单元格的catViewController 上设置data 时实际发生的情况。如何优化很大程度上取决于您在该方法中所做的工作。
  • 也许你需要的是一个 PageViewController。

标签: ios uitableview asynchronous uicollectionview uicollectionviewcell


【解决方案1】:

对于高性能 CollectionView 单元格,在 iOS10 和 xcode8

中使用以下新内容

在你的 ViewController 中实现协议 “UICollectionViewDataSourcePrefetching”

类 ViewController:UIViewController、UICollectionViewDataSourcePrefetching {

在情节提要中将以下代表设置到您的集合视图(参见附图) 或以编程方式

ViewController的 viewDidLoad方法中

collectionView.delegate = self

collectionView.dataSource = self

collectionView.prefetchDataSource = self

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-06
    • 2013-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多