Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

detecting when an iOS UICollectionCell is going off screen

I'm having a UICollectionView that holds pictures as elements in its datastore.

I want to load a high resolution pic into the element only when its corresponding UICollectionViewCell is currently showing on screen. Later, when the UICollectionViewCell goes off screen, I want to return the element's UIImage into the low resolution version.

My question is, how can I detect when a UICollectionViewCell is going off screen?

(I tried using the prepareForReuse method but I can't predict when it will be called).

I'm currently using a piece of code that sits in scrollViewDidScroll, and every time that the view scrolls I'm checking the self.collectionView.visibleCells to see which cells has scrolled off screen.

It seems a bit of an overhead and I wonder if there is a method called on the UICollectionViewCell itself whenever it is being scrolled of screen ?

like image 759
Boaz Saragossi Avatar asked Aug 27 '13 12:08

Boaz Saragossi


2 Answers

From Documentation. collectionView:didEndDisplayingCell is called right after it finishes displaying, not when it goes off screen

Use this method to detect when a cell is removed from a collection view, as opposed to monitoring the view itself to see when it disappears

like image 69
Yesbol Kulanbekov Avatar answered Oct 27 '22 01:10

Yesbol Kulanbekov


The collectionView:didEndDisplayingCell:forItemAtIndexPath: method on UICollectionViewDelegate should do what you want.

like image 20
Mike Weller Avatar answered Oct 27 '22 00:10

Mike Weller