集合视图,自定义布局,单元格在滚动时行为不正常 [英] Collection View,with custom layouts, cells misbehave on scrolling

查看:101
本文介绍了集合视图,自定义布局,单元格在滚动时行为不正常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to create custom tiled layout using UICollectionView. It renders perfectly as desired in simulator once I run my app.

但是当我滚动视图并将其恢复所有单元格的框架更改和细胞重叠,留下空格,随机。

But the moment I scroll the view and bring it back all the cell's frame changes and the cells get overlapped, leaving spaces, randomly.

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

I am not able to solve this issue past 2 days. Here goes the code from my custom 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;
}

请提前帮助,谢谢。

编辑
在我的 [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:,看看类似这样的事情

While I modified layoutAttributesForItemAtIndexPath:, to look something like this

-(UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath{
 UICollectionViewLayoutAttributes * layoutAttributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
 layoutAttributes.frame = ((UICollectionViewLayoutAttributes*)[attributeArrayOfAll objectAtIndex:indexPath.item]).frame;
 return layoutAttributes;
}

此外,方法 layoutAttributesForItemAtIndexPath:永远不会被隐式调用。我甚至试过这个:

Moreover, the method layoutAttributesForItemAtIndexPath: never gets 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;
}

但结果仍然是滚动时相同的扭曲细胞集。
我使用5个单元格,第一次正确渲染,滚动然后将其带回可见的矩形会变形,如果我再次滚动并将其带回可见的矩形,它会正确渲染。但是,当我用大约400个单元格执行此操作时,一旦我滚动它永远不会正确呈现。即使在重新加载集合视图时,单元格也会变形。请帮忙。

But still the result is the same distorted set of cells on scrolling. I worked with 5 cells, first time it renders correctly, on scrolling away and then bringing it back in visible rect it gets distorted, if i scroll away again and bring it back in visible rect it renders correctly. However, when I do this with around 400 cells, once i scroll it never renders correctly. Even on reloading collection view, The cells gets distort. Please help.

推荐答案

所以最后管理了一个解决方法!使用cellForRow中的唯一单元标识符将每个单元格出列:

So finally managed a workaround!!! dequeue each cell with an unique Cell Identifier in 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个单元格它增加了我的申请规模相当大。让希望苹果尽快解决这个问题。

These two lines inside cellForRow worked for me, however with my collection view having around 1000 cells it increases the size of my application considerably. Lets hope apple fixes this bug asap.

这篇关于集合视图,自定义布局,单元格在滚动时行为不正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆