0
votes

There are a lot of answers out there for loading images into UITableViews or UICollectionViews. But what if my UICollectionView is displaying views from other view controllers?

Say in my UICollectionViewCell subclass I have this:

- (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;
}

And in my collection view Datasource:

- (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;
}

The catViewController has a setter for the data property. When this property is set, the cat will load it's image, along with some other related images for that view. So how do I properly reuse the MyCell cells so that the collection view doesn't stutter each time it creates (or reuses) a cell? Each MyCell takes up the full width of the collection view, which scrolls horizontally, so every time a new cell scrolls into view, the collection view stalls for a moment.

1
If setting up the cell takes too much time, you may want to populate dummy cells in the mean time, and load the image in the completion block of you data setter. That way you'll see the cell fade in over a placeholder when it has loaded, but scrolling can remain smooth. - Kevin DiTraglia
I think you need to post what actually happens when you set data on the cell's catViewController. How to optimize is highly dependent on what you're doing in that method. - Christopher Pickslay
Maybe what you need is a PageViewController. - Gil Sand

1 Answers

0
votes

For High performance CollectionView cells , Use following new stuffs in iOS10 with xcode8

Implement protocol "UICollectionViewDataSourcePrefetching" in you ViewController as

class ViewController: UIViewController , UICollectionViewDataSourcePrefetching {

Set following delegates to your collection view in storyboard (see the attached image) or programmatically

In ViewController's viewDidLoad method

collectionView.delegate = self

collectionView.dataSource = self

collectionView.prefetchDataSource = self enter image description here