简体   繁体   English

如何确定何时显示UICollectionViewCell?

[英]How can I determine when a UICollectionViewCell is displayed?

I'd like to know when a UICollectionViewCell is displayed on the actual screen. 我想知道什么时候在实际屏幕上显示UICollectionViewCell cellForRowAtIndexPath is insufficient, as the cell isn't actually seen at this point. cellForRowAtIndexPath不足,因为此时实际上没有看到该单元格。 didEndDisplayingCell is insufficient, as it gets called when the cell is removed from view. didEndDisplayingCell不足,因为从视图中删除单元格时会调用它。

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 . UITableViewDelegate有一个willDisplayCell方法,我发现它在过去对类似的东西很有用,但它似乎不存在于UICollectionViewDelegate

How can I determine when the cell is brought on to the screen? 如何确定电池何时被带到屏幕上?

Make a subclass of UICollectionViewCell . UICollectionViewCell的子类。 Override its didMoveToWindow method: 覆盖其didMoveToWindow方法:

- (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. (我知道UITableView使用iOS 6.0中的表格视图单元格。)在这种情况下,当单元格重新用于另一个项目时,您将不会收到didMoveToWindow消息。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何确定自定义UICollectionViewCell何时在屏幕上100% - How to determine when a custom UICollectionViewCell is 100% on the screen 如何检测 UICollectionViewCell 的中点何时在 UICollectionView 的框架之外? - How can I detect when the midpoint of a UICollectionViewCell is outside the frame of the UICollectionView? 如何确定是否显示后退按钮? - How can I determine whether a back button is displayed? 我如何在UICollectionViewCell中绕过UIImageView? - How I can round UIImageView in UICollectionViewCell? iOS:如何更改UICollectionViewCell的方向? - iOS: How can I change the orientation of a UICollectionViewCell? 如何获取UICollectionViewCell的indexPath并保存 - How can I get the indexPath of a UICollectionViewCell and save it 如何在UICollectionViewCell中居中UIActivityIndi​​catorView? - How can I center a UIActivityIndicatorView in a UICollectionViewCell? 当UICollectionView滚动时,UICollectionViewCell中的subView显示错误 - subView in UICollectionViewCell displayed wrong when UICollectionView scrolled 滚动时,大型UICollectionViewCell停止显示 - Large UICollectionViewCell stopped being displayed when scrolling UICollectionViewCell:我能确切知道单元格何时出现和消失吗? - UICollectionViewCell: can I know exactly when the cell appears and disappears?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM