2

我正在尝试使用 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 个单元格执行此操作时,一旦我滚动它就永远不会正确呈现。即使在重新加载集合视图时,单元格也会变形。请帮忙。

4

2 回答 2

3

您的layoutAttributesForItemAtIndexPath:方法layoutAttributes在返回之前没有设置对象的任何属性。它需要设置frame(或centersize)。

于 2013-06-03T14:21:14.390 回答
0

所以终于设法解决了!在 cellForRow 中使用唯一的单元标识符使每个单元出列:

[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 个单元格,它大大增加了我的应用程序的大小。让我们希望苹果尽快修复这个错误。

于 2013-06-05T09:42:38.747 回答