我已经创建了自定义布局子类 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/