UICollectionView flowLayout没有正确包装单元格(iOS) [英] UICollectionView flowLayout not wrapping cells correctly (iOS)

查看:81
本文介绍了UICollectionView flowLayout没有正确包装单元格(iOS)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有FLowLayout的 UICollectionView 。它会像我期望的那样工作,但是偶尔会有一个单元格无法正确包裹。例如,如果实际上在第二行中尾随,则应该在第三行的第一个列中打开的单元格,并且它应该是一个空的空间(参见下图)。你可以看到这个胭脂细胞的左侧(其余部分被切断),它应该是空的。

I have a UICollectionView with a FLowLayout. It will work as I expect most of the time, but every now and then one of the cells does not wrap properly. For example, the the cell that should be on in the first "column" of the third row if actually trailing in the second row and there is just an empty space where it should be (see diagram below). All you can see of this rouge cell is the left hand side (the rest is cut off) and the place it should be is empty.

这不会持续发生;它并不总是同一行。一旦发生,我可以向上滚动然后向后移动,单元格将自行修复。或者,当我按下单元格(通过按钮将我带到下一个视图)然后弹回时,我会看到单元格位于错误的位置,然后它将跳转到正确的位置。

This does not happen consistently; it is not always the same row. Once it has happened, I can scroll up and then back and the cell will have fixed itself. Or, when I press the cell (which takes me to the next view via a push) and then pop back, I will see the cell in the incorrect position and then it will jump to the correct position.

滚动速度似乎可以更容易地重现问题。当我慢慢滚动时,我仍然可以偶尔看到单元格处于错误的位置,但是它会立即跳到正确的位置。

The scroll speed seems to make it easier to reproduce the problem. When I scroll slowly, I can still see the cell in the wrong position every now and then, but then it will jump to the correct position straight away.

问题已经开始当我添加部分插入。以前,我的细胞几乎与集合边界齐平(很少或没有插入),我没有注意到这个问题。但这意味着集合视图的右侧和左侧是空的。即,无法滚动。此外,滚动条没有向右冲洗。

The problem started when I added the sections insets. Previously, I had the cells almost flush against the collection bounds (little, or no insets) and I did not notice the problem. But this meant the right and left of the collection view was empty. Ie, could not scroll. Also, the scroll bar was not flush to the right.

我可以在模拟器和iPad 3上发生问题。

I can make the problem happen on both Simulator and on an iPad 3.

我猜问题正在发生,因为左右部分插入...但如果值是错误的,那么我希望行为是一致的。我想知道这可能是Apple的错误吗?或者这可能是由于插件的堆积或类似的东西......

I guess the problem is happening because of the left and right section insets... But if the value is wrong, then I would expect the behavior to be consistent. I wonder if this might be a bug with Apple? Or perhaps this is due to a build up of the insets or something similar...

任何解决方案/解决方法都很受欢迎。

Any solutions/workarounds are appreciated.

跟进:我一直在使用这个答案以下尼克超过 6个月 12个月 2年现在没有问题(如果有人想知道答案中是否有任何漏洞 - 我还没有找到任何)。干得好。

Follow up: I have been using this answer below by Nick for over 6 months 12 months 2 years now without a problem (in case people are wondering if there are any holes in that answer - I have not found any yet). Well done Nick.

推荐答案

UICollectionViewFlowLayout的layoutAttributesForElementsInRect实现中存在一个错误,导致它返回单个单元格的两个属性对象在涉及部分插入的某些情况下。其中一个返回的属性对象无效(在集合视图的边界之外),另一个是有效的。下面是UICollectionViewFlowLayout的子类,通过排除集合视图边界之外的单元格来解决问题。

There is a bug in UICollectionViewFlowLayout's implementation of layoutAttributesForElementsInRect that causes it to return TWO attribute objects for a single cell in certain cases involving section insets. One of the returned attribute objects is invalid (outside the bounds of the collection view) and the other is valid. Below is a subclass of UICollectionViewFlowLayout that fixes the problem by excluding cells outside of the collection view's bounds.

// NDCollectionViewFlowLayout.h
@interface NDCollectionViewFlowLayout : UICollectionViewFlowLayout
@end

// NDCollectionViewFlowLayout.m
#import "NDCollectionViewFlowLayout.h"
@implementation NDCollectionViewFlowLayout
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {
  NSArray *attributes = [super layoutAttributesForElementsInRect:rect];
  NSMutableArray *newAttributes = [NSMutableArray arrayWithCapacity:attributes.count];
  for (UICollectionViewLayoutAttributes *attribute in attributes) {
    if ((attribute.frame.origin.x + attribute.frame.size.width <= self.collectionViewContentSize.width) &&
        (attribute.frame.origin.y + attribute.frame.size.height <= self.collectionViewContentSize.height)) {
      [newAttributes addObject:attribute];
    }
  }
  return newAttributes;
}
@end

参见这个

其他答案建议从shouldInvalidateLayoutForBoundsChange返回YES,但这会导致不必要的重新计算并且不会甚至完全解决了这个问题。

Other answers suggest returning YES from shouldInvalidateLayoutForBoundsChange, but this causes unnecessary recomputations and doesn't even completely solve the problem.

我的解决方案完全解决了这个问题,并且在Apple修复根本原因时不应该导致任何问题。

My solution completely solves the bug and shouldn't cause any problems when Apple fixes the root cause.

这篇关于UICollectionView flowLayout没有正确包装单元格(iOS)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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