I am trying to create a custom broken layout using a UICollectionView. When I launch my application, it performs its best in the simulator.
But at the moment when I look at the view and return it all changes to the cell frame, and the cells overlap, leaving spaces at random.
I can not solve this problem in the last 2 days. Here is the code from my own layout class.
-(void)prepareLayout{ [self createCellSizeArray];//cellSizeArray holds cell sizes for all the cells(calculated statically) [self createAttributeArrayOfAll];//attributeArrayOfAll holds attributes for all the cells and also calculates their frames using cellSizeArray } -(CGSize)collectionViewContentSize{ return CGSizeMake(768, 1500);//The size is static to check for scrolling, is this creating problems? } -(UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath{ UICollectionViewLayoutAttributes * layoutAttributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath]; return layoutAttributes; } -(NSArray *)layoutAttributesForElementsInRect:(CGRect)rect{ NSMutableArray *attArray =[NSMutableArray array]; for (NSInteger i =0; i< attributeArrayOfAll.count; i++) { UICollectionViewLayoutAttributes * attribute = (UICollectionViewLayoutAttributes*)[attributeArrayOfAll objectAtIndex:i]; if(CGRectIntersectsRect(rect, attribute.frame)){ [attArray addObject:attribute]; } } return attArray; } -(BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds{ return YES; }
Please help in advance.
Edit : In my [self createAttributeArrayOfAll]; I have these lines of code
CGRect frame = CGRectMake(_startNewRowPoint.x, _startNewRowPoint.y, cellSize.width, cellSize.height); UICollectionViewLayoutAttributes * attribute = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:[NSIndexPath indexPathForItem:i inSection:0]]; attribute.alpha = 1.0; attribute.frame = frame; [attributeArrayOfAll addObject:attribute];
So far I've been layoutAttributesForItemAtIndexPath: to see something like this
-(UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath{ UICollectionViewLayoutAttributes * layoutAttributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath]; layoutAttributes.frame = ((UICollectionViewLayoutAttributes*)[attributeArrayOfAll objectAtIndex:indexPath.item]).frame; return layoutAttributes; }
In addition, the layoutAttributesForItemAtIndexPath: method will never be called implicitly. I even tried this:
-(NSArray *)layoutAttributesForElementsInRect:(CGRect)rect{ NSMutableArray *attArray =[NSMutableArray arrayWithCapacity:attributeArrayOfAll.count]; for (NSInteger i =0; i< attributeArrayOfAll.count; i++) { UICollectionViewLayoutAttributes * attribute = (UICollectionViewLayoutAttributes*)[attributeArrayOfAll objectAtIndex:i]; if(CGRectIntersectsRect(rect, attribute.frame)){ [attArray insertObject:[self layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForItem:i inSection:0]] atIndex:i]; } } return attArray; }
But still, the result is the same distorted set of cells when scrolling. I worked with 5 cells, the first time it is displayed correctly, when scrolling and then returning to the visible rectangle, it is distorted if I scroll away again and return it to the visible rectangle that it displays correctly. However, when I do this with approximately 400 cells, as soon as I scroll through it, it will never display correctly. Even when the collection is overloaded, the cells are distorted. Please, help.