Detection when iOS UICollectionCell leaves the screen

I have a UICollectionView that contains images as elements in my data store.

I want to load a high resolution image into an element only when its corresponding UICollectionViewCell is currently displayed on the screen. Later, when the UICollectionViewCell disabled, I want to return the UIImage element to the low resolution version.

My question is: how can I detect when a UICollectionViewCell comes out of the screen?

(I tried to use the prepareForReuse method, but I cannot predict when it will be called).

I am currently using a piece of code that is in scrollViewDidScroll , and each time the view scrolls, I check self.collectionView.visibleCells to see which cells scroll from the screen.

It seems like a bit of overhead, and I wonder if there is a method called UICollectionViewCell itself when it scrolls on the screen?

+8
ios uicollectionview uicollectionviewcell uicollectionreusableview
source share
2 answers

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

+24
source share

From the documentation. collectionView: didEndDisplayingCell is called immediately after the display is complete, and not when it is turned off.

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

0
source share

All Articles