自定义 UICollectionViewLayout:layoutAttributesForElementsInRect 要求非常小的矩形

Mat*_*oal 3 ios uicollectionview

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

这是我的 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;
}
Run Code Online (Sandbox Code Playgroud)

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

仅当我的集合视图中有几个单元格时才会发生这种情况。

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

Mat*_*oal 5

问题出在collectionViewContentSize. 我在这个函数中有一个错误,当单元格数量很少时,它返回的宽度为 0。