【问题标题】:How to get the rect of a UICollectionViewCell?如何获取 UICollectionViewCell 的矩形?
【发布时间】:2012-09-12 08:45:20
【问题描述】:

UITableViewrectForRowAtIndexPath: 方法,但在 UICollectionView 中不存在此方法。我正在寻找一种干净的方法来获取单元格的边界矩形,也许我可以将其添加为UICollectionView 上的类别。

【问题讨论】:

    标签: ios uitableview ios6 uicollectionview


    【解决方案1】:

    我发现最好的方法如下:

    Objective-C

    UICollectionViewLayoutAttributes *attributes = [self.collectionView layoutAttributesForItemAtIndexPath:indexPath];
    

    斯威夫特

    let attributes = collectionView.layoutAttributesForItem(at: indexPath)
    

    然后您可以通过attributes.frameattributes.center 访问该位置

    【讨论】:

    • 这将给出与superview相关的单元格的绝对位置CGRect cellFrameInSuperview = [self.collectionView convertRect:attributes.frame toView:[self.collectionView superview]];
    【解决方案2】:

    只需要两行代码即可获得完美的框架:

    Objective-C

    UICollectionViewLayoutAttributes * theAttributes = [collectionView layoutAttributesForItemAtIndexPath:indexPath];
    
    CGRect cellFrameInSuperview = [collectionView convertRect:theAttributes.frame toView:[collectionView superview]];
    

    Swift 4.2

    let theAttributes = collectionView.layoutAttributesForItem(at: indexPath)
    let cellFrameInSuperview = collectionView.convert(theAttributes.frame, to: collectionView.superview)
    

    【讨论】:

    • 这就是我要找的,它会在滚动时为您提供更新的框架。
    【解决方案3】:

    在 Swift 3 中

     let theAttributes:UICollectionViewLayoutAttributes! = collectionView.layoutAttributesForItem(at: indexPath)
     let cellFrameInSuperview:CGRect!  = collectionView.convert(theAttributes.frame, to: collectionView.superview)
    

    【讨论】:

      【解决方案4】:

      你可以迅速做到:

      //for any cell in collectionView
      let rect = self.collectionViewLayout.layoutAttributesForItemAtIndexPath(clIndexPath).frame
      
      //if you only need for visible cells
      let rect = cellForItemAtIndexPath(indexPath)?.frame
      

      【讨论】:

        【解决方案5】:

        怎么样

        -(CGRect)rectForCellatIndexPath:(NSIndexPath *)indexPath {
        
            UICollectionViewCell *cell = [self cellForItemAtIndexPath:indexPath];
        
            if (!cell) {
                return CGRectZero;
            }
        
            return cell.frame;
        
        }
        

        作为UICollectionView上的一个类别?

        #import <UIKit/UIKit.h>
        
        @interface UICollectionView (CellFrame)
        
        -(CGRect)rectForCellatIndexPath:(NSIndexPath *)indexPath;
        
        @end
        
        
        #import "UICollectionView+CellFrame.h"
        
        @implementation UICollectionView (CellFrame)
        
        -(CGRect)rectForCellatIndexPath:(NSIndexPath *)indexPath {
        
            UICollectionViewCell *cell = [self cellForItemAtIndexPath:indexPath];
        
            if (!cell) {
                return CGRectZero;
            }
        
            return cell.frame;
        
        }
        
        @end
        

        【讨论】:

        • 这可行,但会给出单元格的框架,因为它位于其自己的视图层次结构中(意味着每个单元格的原点都是 {0,0})。我需要知道表格视图中的矩形是什么。
        • 使用UIViewconvertRect:toView:方法将rect相对于collection view进行转换。
        • cellForItemAtIndexPath 路径仅返回可见单元格的值。如果 indexPath 处的单元格不在屏幕上,则返回 nil。
        • 适用于可见单元格,并且 OP 没有指定可见单元格。
        • 仅适用于可见单元格!
        猜你喜欢
        • 2018-11-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-11
        相关资源
        最近更新 更多