3
votes

I've created my custom layout subclassing UICollectionViewLayout. It works exactly as I need but when the collection has only a little number of elements (like "3") the method layoutAttributesForElementsInRect asks information for a rectangle with 0 width.

This is the code of my layoutAttributesForElementsInRect implementation:

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

    NSMutableArray *results = [NSMutableArray array];

    for (UICollectionViewLayoutAttributes *attributes in self.elementsAttributes) {

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

    return results;
}

I cache the attributes in the prepareLayout method... so for example I have 3 elements, but the rect parameter that I receive from the previous function is just "impossibile", indeed it is: {{0, 0}, {0, 180}}.

This happens only when I have a few cells in my collection view.

Why and how to get attributes for all the cells that should be displayed?

1

1 Answers

5
votes

The problem was in the override of collectionViewContentSize. I had a bug in this function and with a small number of Cells it returns a width of 0.