I have a paged UICollectionView, with one cell per page. However, my UICollectionViewCells are slow to render (third party library, no options there). So as an optimisation, i need to pre-render the cells to the left and right of the current page, so scrolling is smooth.

What i've done is, inside my custom UICollectionViewLayout, is outset the passed rect by 1 point before checking to see which cells' UICollectionViewLayoutAttributes should be returned, see here:

- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {
    CGRect outsetRect = CGRectInset(rect, -1, 0);
    return [_myAttributes filter:^BOOL(UICollectionViewLayoutAttributes *attribs) {
        return CGRectIntersectsRect(attribs.frame, outsetRect);
    }];
}

This works as expected. However, when the actual UICollectionView does its thing, it seemingly ignores the offscreen cells and calls cellForItemAtIndexPath only for the first cell, not the cell to the right (or left).

Any ideas what is going on? Thanks

有帮助吗?

解决方案

For future reference, i solved this by:

  • Making cells for pre-rendering forcibly moved on-screen a little, but with the same size.
  • Changing their z-index to -1 so they are under the main page cell.
  • In the layoutAttributesForElementsInRect, check if the rect is two pages wide, and if so then it's scrolling between pages, so don't grab any extra pages
  • In my cell's applyLayoutAttributes, i check if the z-index<0, and if so, then this is a pre-rendering cell.

Cheers

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top