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

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

问题描述

我正在尝试使用 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]; 我有这些代码行

Edit: In my [self createAttributeArrayOfAll]; I have these lines of code

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天全站免登陆