UICollectionView、UITableView或者某一个cell的位置

UICollectionView:
一般我们获取cell的位置是如下方式获取:

UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:indexPath];
CGRect cellRect = cell.frame;

但是这样获取cell有时候会获取到为nil的情况;比如当cell滑出屏幕不可见时。

可以采用以下方式获取cell的frame,不管是可见还是不可见。使用cell的layoutattribute属性获取

UICollectionViewLayoutAttributes *att = [self.collectionView layoutAttributesForItemAtIndexPath:indexPath];
CGRect cellRect = att.frame;

UITableView:
一般也是下面这种方式获取

UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:indexPath];
CGRect cellRect = cell.frame;

也会存在cell为nil的情况,即cell不在屏幕内时,可以用下面的方法获取

(CGRect)rectForRowAtIndexPath:(NSIndexPath *)indexPath
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。