Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UICollectionView flowLayout not wrapping cells correctly

I have a UICollectionView with a FLowLayout. It will work as I expect most of the time, but every now and then one of the cells does not wrap properly. For example, the the cell that should be on in the first "column" of the third row if actually trailing in the second row and there is just an empty space where it should be (see diagram below). All you can see of this rouge cell is the left hand side (the rest is cut off) and the place it should be is empty.

This does not happen consistently; it is not always the same row. Once it has happened, I can scroll up and then back and the cell will have fixed itself. Or, when I press the cell (which takes me to the next view via a push) and then pop back, I will see the cell in the incorrect position and then it will jump to the correct position.

The scroll speed seems to make it easier to reproduce the problem. When I scroll slowly, I can still see the cell in the wrong position every now and then, but then it will jump to the correct position straight away.

The problem started when I added the sections insets. Previously, I had the cells almost flush against the collection bounds (little, or no insets) and I did not notice the problem. But this meant the right and left of the collection view was empty. Ie, could not scroll. Also, the scroll bar was not flush to the right.

I can make the problem happen on both Simulator and on an iPad 3.

I guess the problem is happening because of the left and right section insets... But if the value is wrong, then I would expect the behavior to be consistent. I wonder if this might be a bug with Apple? Or perhaps this is due to a build up of the insets or something similar.

Illustration of problem and settings


Follow up: I have been using this answer below by Nick for over 2 years now without a problem (in case people are wondering if there are any holes in that answer - I have not found any yet). Well done Nick.

like image 507
lindon fox Avatar asked Oct 17 '12 04:10

lindon fox


2 Answers

There is a bug in UICollectionViewFlowLayout's implementation of layoutAttributesForElementsInRect that causes it to return TWO attribute objects for a single cell in certain cases involving section insets. One of the returned attribute objects is invalid (outside the bounds of the collection view) and the other is valid. Below is a subclass of UICollectionViewFlowLayout that fixes the problem by excluding cells outside of the collection view's bounds.

// NDCollectionViewFlowLayout.h @interface NDCollectionViewFlowLayout : UICollectionViewFlowLayout @end  // NDCollectionViewFlowLayout.m #import "NDCollectionViewFlowLayout.h" @implementation NDCollectionViewFlowLayout - (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {   NSArray *attributes = [super layoutAttributesForElementsInRect:rect];   NSMutableArray *newAttributes = [NSMutableArray arrayWithCapacity:attributes.count];   for (UICollectionViewLayoutAttributes *attribute in attributes) {     if ((attribute.frame.origin.x + attribute.frame.size.width <= self.collectionViewContentSize.width) &&         (attribute.frame.origin.y + attribute.frame.size.height <= self.collectionViewContentSize.height)) {       [newAttributes addObject:attribute];     }   }   return newAttributes; } @end 

See this.

Other answers suggest returning YES from shouldInvalidateLayoutForBoundsChange, but this causes unnecessary recomputations and doesn't even completely solve the problem.

My solution completely solves the bug and shouldn't cause any problems when Apple fixes the root cause.

like image 150
Nick Snyder Avatar answered Sep 18 '22 09:09

Nick Snyder


Put this into the viewController that owns the collection view

- (void)viewWillLayoutSubviews {     [super viewWillLayoutSubviews];     [self.collectionView.collectionViewLayout invalidateLayout]; } 
like image 45
Peter Lapisu Avatar answered Sep 20 '22 09:09

Peter Lapisu