Reputation: 1132
I'd like to know when a UICollectionViewCell
is displayed on the actual screen. cellForRowAtIndexPath
is insufficient, as the cell isn't actually seen at this point. didEndDisplayingCell
is insufficient, as it gets called when the cell is removed from view.
UITableViewDelegate
has a willDisplayCell
method that I've found useful for similar stuff in the past, but it doesn't appear to exist in UICollectionViewDelegate
.
How can I determine when the cell is brought on to the screen?
Upvotes: 8
Views: 5056
Reputation: 385610
Make a subclass of UICollectionViewCell
. Override its didMoveToWindow
method:
- (void)didMoveToWindow {
if (self.window != nil) {
// I am now in the window's view hierarchy and thus “on screen”.
}
}
Technically, the cell could still be not visible, either because it's outside the window's visible bounds, or because it's covered by another view. But normally neither of those will be the case.
Note also that if a cell is reused, it might not get removed from and re-added to the view hierarchy. The collection view can just change the cell's frame. (I know UITableView
does this with table view cells as of iOS 6.0.) In that case, you won't receive a didMoveToWindow
message when the cell gets re-used for another item.
If you explain why you want to know when a cell is displayed, we might be able to give you a better answer.
Upvotes: 6
Reputation: 3991
-[UICollectionView visibleCells]
returns a NSArray of all cells visible on screnn
Upvotes: 1