美文网首页
UICollectionView、UITableView或者某一

UICollectionView、UITableView或者某一

作者: 李Mr | 来源:发表于2020-07-16 18:44 被阅读0次

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

相关文章

网友评论

      本文标题:UICollectionView、UITableView或者某一

      本文链接:https://www.haomeiwen.com/subject/cgjmhktx.html