UICollectionView Very Slow User Layout

I use a UICollectionView with a custom layout that exposes the cells as a grid. There can be more than 50 rows and 50 columns. Scrolling occurs both vertically and horizontally. I am currently doing the layout setup in prepareLayout and storing it in arrays:

 - (void)prepareLayout { NSMutableArray *newLayoutInfo = [[NSMutableArray alloc] init]; NSMutableArray *newLinearLayoutInfor = [[NSMutableArray alloc] init]; NSInteger sectionCount = [self.collectionView numberOfSections]; NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:0]; self.heightForRows = [delegate collectionViewHeightForAllRows]; self.totalWidthsForRows = [[NSMutableArray alloc] init]; for (int i = 0; i < sectionCount; i++) { [self.totalWidthsForRows addObject:[NSNumber numberWithInt:0]]; } for (NSInteger section = 0; section < sectionCount; section++) { NSMutableArray *cellLayoutInfo = [[NSMutableArray alloc] init]; NSInteger itemCount = [self.collectionView numberOfItemsInSection:section]; for (NSInteger item = 0; item < itemCount; item++) { indexPath = [NSIndexPath indexPathForItem:item inSection:section]; UICollectionViewLayoutAttributes *itemAttributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath]; itemAttributes.frame = [self frameForCellAtIndexPath:indexPath]; [cellLayoutInfo addObject:itemAttributes]; [newLinearLayoutInfor addObject:itemAttributes]; } [newLayoutInfo addObject:cellLayoutInfo]; } self.layoutInfo = newLayoutInfo; self.linearLayoutInfo = newLinearLayoutInfor; } 

Then in layoutAttributesForElementsInRect I:

 - (NSArray*)layoutAttributesForElementsInRect:(CGRect)rect { NSArray *rows = [self.linearLayoutInfo filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(UICollectionViewLayoutAttributes *evaluatedObject, NSDictionary *bindings) { return CGRectIntersectsRect(rect, [evaluatedObject frame]); }]]; 

This works fine, but it is backward and nervous when I have more than 50 columns and 50 rows. The problem that I have now is that I have to install

 -(BOOL)shouldInvalidateLayoutForBoundsChange { return YES; } 

This forces him to cook the entire layout every time the boundaries change, which, of course, has a huge impact on performance, and you can barely scroll. The cells consist only of text with an opaque background, so there are no problems there.

I am sure that I am not doing it right and that there should be a better way. Thanks for your help in advance.

+7
source share
3 answers

In a custom thread layout, I do this and it seems to help:

 - (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds { return !(CGSizeEqualToSize(newBounds.size, self.collectionView.frame.size)); } 
+9
source
 -(BOOL)shouldInvalidateLayoutForBoundsChange { return YES; } 

It prepareLayout() layout to do prepareLayout() every time it scrolls, which means that any of the heavy calculations in preparation will lead to practice camps, so one of the possible ways to solve this problem is to verify that it actually takes a lot of time. One of the possibilities is that inside

 for (NSInteger section = 0; section < sectionCount; section++) { // generate attributes ... } 

to generate layout attributes. Each time it scrolls, each time this generalization is repeated, so that it acts on the scroll, it seems nervous and awkward. Therefore, in order to solve this problem, or at least decide that it is not a problem, I suggest setting a flag in this layout algorithm, say, isScrolling, for the situation when the layout needs to be prepared. Every time in prepareLayout() check the flag, if it is YES , then we will know that there is no need to do for the loop to restore all the attributes that alreay exsit is initialized from the very beginning of the layout.

+5
source

ok - Now I understand. Here I recommend: create 3 collection views ... one for column headings (where each cell is a column heading), one for row leaders (each cell = 1 row) and one collection view for your cells. Then, when the user views the scroll position of any kind of collection, update the scroll positions for the other views of collection 2, if necessary.

enter image description here

0
source

All Articles