集合视图,具有自定义布局,单元格在滚动时行为异常

2023-12-02

我正在尝试使用 UICollectionView 创建自定义平铺布局。 一旦我运行我的应用程序,它就会在模拟器中完美呈现。

但是当我滚动视图并将其带回时,所有单元格的框架都会发生变化,并且单元格会重叠,随机留下空格。

过去两天我无法解决这个问题。 这是我的自定义布局类中的代码。

-(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;
}

请帮忙,提前致谢。

Edit: In my [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 个单元格执行此操作时,一旦滚动,它就永远无法正确渲染。即使在重新加载集合视图时,单元格也会变形。请帮忙。


Your layoutAttributesForItemAtIndexPath:方法没有设置任何属性layoutAttributes返回对象之前。需要设置一下frame (or center and size).

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

集合视图,具有自定义布局,单元格在滚动时行为异常 的相关文章

随机推荐