detecting when an iOS UICollectionCell is going off screen

Boaz Saragossi picture Boaz Saragossi · Aug 27, 2013 · Viewed 7.5k times · Source

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 ?

Answer

Mike Weller picture Mike Weller · Aug 27, 2013

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