InfinityLoop
InfinityLoop

Reputation: 429

UICollectionView, Images inside a cell does not load until the cell is scrolled off screen

Using Xcode 6 beta 6, I have used a standard View Controller and placed a Collection view into it. Following this, I have added a prototype cell from which an array on the CollectionViewController.m file provides the images.

While the images do appear, they do not load until after that cell has been scrolled off the screen and then back on. This is stopping any images from loading when the app opens until the user scrolls down.

The images are stored locally in the file, not a Database.

Despite the lack of images, I have linked the cells to another view controller, and even though no image is displayed, the segue still operates.

Thanks for any help in advance.

Upvotes: 1

Views: 1273

Answers (2)

Milad Cyrus
Milad Cyrus

Reputation: 11

you should use willDisplayCell method to assign image. first initial image then create cell.

here is the code :

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
return cell;}



-(void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath{

UIImageView *imageView = (UIImageView *)[cell viewWithTag:100];
imageView.image = images[indexPath.row];}

And don't forget to reload collection view after your data is ready to be loaded:

[_myCollection reloadData];

Upvotes: 1

Michi
Michi

Reputation: 1484

I guess you are setting the UIImage on the cell image view out of the main thread. Wrap the setter code into a following snippet:

[[NSOperationQueue mainQueue] addOperationWithBlock:^{

    cell.imageView.image = {UIImage object you've got};

}];

Optionally you could also try different, single-line replacement approach, if possible:

[cell.imageView performSelectorOnMainThread:@selector(setImage:) withObject:{UIImage object} waitUntilDone:NO];

I can't find what kind of cell you mean in fact; in every case, apply the same on a particular target image view, no matter if it's a part of your cell as a property or not.

Upvotes: 2

Related Questions