How to determine when a UICollectionViewCell is displayed?

I would like to know when a UICollectionViewCell displayed on the current screen. cellForRowAtIndexPath not enough, because at the moment this cell is actually not visible. didEndDisplayingCell not enough as it is called when the cell is removed from the view.

UITableViewDelegate has a willDisplayCell method, which I found useful for similar things in the past, but it doesn't seem to exist in UICollectionViewDelegate .

How to determine when a cell is turned on?

+6
source share
2 answers

Subclass UICollectionViewCell . Override the didMoveToWindow method:

 - (void)didMoveToWindow { if (self.window != nil) { // I am now in the window view hierarchy and thus "on screen". } } 

Technically, a cell may still not be visible, either because it is outside the visible borders of the window, or because it is covered by another view. But usually this is not so.

Note also that if a cell is reused, it cannot be deleted and re-added to the view hierarchy. A collection view can simply change the frame of a cell. (I know that a UITableView does this with table cells with iOS 6.0.) In this case, you will not receive a didMoveToWindow message when the cell is reused for another element.

If you explain why you want to know when a cell is displayed, we could give you a better answer.

+4
source

-[UICollectionView visibleCells] returns an NSArray of all cells visible on screnn

0
source

Source: https://habr.com/ru/post/948093/


All Articles