集合视图,自定义布局,单元格在滚动时行为不正常
我正在尝试使用UICollectionView创建自定义平铺布局。 一旦我运行我的应用程序,它在模拟器中完美呈现。
但是,当我滚动视图并将其返回所有单元格的框架更改并且单元格重叠,随机留下空间时。
我过去2天无法解决这个问题。 这里是我自定义布局类的代码。
-(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; }
请帮助,提前致谢。
编辑 :在我的[self createAttributeArrayOfAll];
我有这些代码行
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];
虽然我修改了layoutAttributesForItemAtIndexPath:
看起来像这样
-(UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath{ UICollectionViewLayoutAttributes * layoutAttributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath]; layoutAttributes.frame = ((UICollectionViewLayoutAttributes*)[attributeArrayOfAll objectAtIndex:indexPath.item]).frame; return layoutAttributes; }
而且,方法layoutAttributesForItemAtIndexPath:
永远不会被隐式调用。 我甚至试过这个:
-(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; }
但结果仍然是滚动时相同的扭曲细胞集。 我使用5个单元格,第一次正确渲染,滚动然后将其带回可见的矩形,它会变形,如果我再次滚动并将其带回可见的矩形,它会正确渲染。 但是,当我用大约400个单元格执行此操作时,一旦我滚动它永远不会正确呈现。 即使在重新加载集合视图时,单元格也会变形。 请帮忙。
layoutAttributesForItemAtIndexPath:
方法在返回之前未设置layoutAttributes
对象的任何属性。 它需要设置frame
(或center
和size
)。
所以最后管理了一个解决方法! 使用cellForRow中的唯一Cell标识符将每个单元格取列:
[self.summaryView registerClass:[BFSSummaryViewCell class] forCellWithReuseIdentifier:[NSString stringWithFormat:@"%@%d",CellIdentifier,indexPath.row]]; UICollectionViewCell *collectionCell = [collectionView dequeueReusableCellWithReuseIdentifier:[NSString stringWithFormat:@"%@%d",CellIdentifier,indexPath.row] forIndexPath:indexPath];
cellForRow中的这两行为我工作,但是我的集合视图有大约1000个单元格,它大大增加了我的应用程序的大小。 让希望苹果尽快修复这个bug。