ios - 自定义 UICollectionViewLayout : layoutAttributesForElementsInRect asks for really little rect

标签 ios uicollectionview

我已经创建了自定义布局子类 UICollectionViewLayout。它完全按照我的需要工作,但是当集合只有少量元素(如“3”)时,layoutAttributesForElementsInRect 方法会询问有关 0 宽度 的矩形的信息。

这是我的 layoutAttributesForElementsInRect 实现的代码:

- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect{

    NSMutableArray *results = [NSMutableArray array];

    for (UICollectionViewLayoutAttributes *attributes in self.elementsAttributes) {

        if(CGRectIntersectsRect(rect, attributes.frame)){
            [results addObject:attributes];
        }
    }

    return results;
}

我在 prepareLayout 方法中缓存属性...例如我有 3 个元素,但是我从上一个函数接收到的 rect 参数只是“不可能”,确实是:{{0, 0}, {0, 180}}

只有当我的 Collection View 中有几个单元格时才会发生这种情况。

为什么以及如何获取应显示的所有单元格的属性?

最佳答案

问题出在 collectionViewContentSize 的重写中。我在这个函数中有一个错误,对于少量的单元格,它返回宽度为 0。

关于ios - 自定义 UICollectionViewLayout : layoutAttributesForElementsInRect asks for really little rect,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21260993/

相关文章:

ios - 使用 CATransition self.dismiss 时隐藏黑色淡入淡出

ios - 如何在滚动时隐藏 UIView?

ios - 无法使用日期选择器在 tableview 单元格内更新带有新日期的标签

ios - 取消选择当前不可见的单元格

ios - 如何知道在 UICollectionView 中点击了自定义单元格的哪一部分?

ios - 对齐 Collection View 单元格以适合每行 3 个

iOS检测到屏幕左边缘外的拖动

ios - 当每次向 iOS 中的文本字段添加三个数字时,为整数插入一个逗号

ios - 滚动到启用分页的 UICollectionView 中的页面

ios - UICollectionView reloadData 只工作两次