简体   繁体   中英

How can I determine when a UICollectionViewCell is displayed?

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?

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.

-[UICollectionView visibleCells]返回screnn上可见的所有单元格的NSArray

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM