How to get the rect of a UICollectionViewCell?

后端 未结 5 1840
北海茫月
北海茫月 2020-12-23 11:05

UITableView has the method rectForRowAtIndexPath:, but this does not exist in UICollectionView. I\'m looking for a nice clean way to grab a cell\'s

5条回答
  •  旧时难觅i
    2020-12-23 11:43

    How about

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

    as a category on UICollectionView?

    #import 
    
    @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
    

提交回复
热议问题