Uicollectionview,自定义布局,单元格在滚动时行为不端

时间:2013-06-03 14:00:33

标签: ios ipad uicollectionview

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

2 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:0)

所以最终管理了一个解决方法!使用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。

相关问题