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

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

问题描述

我有一个带有 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.

跟进:我使用 Nick 下面的 this answer 已有 2 年多了现在没有问题(如果人们想知道该答案是否有任何漏洞 - 我还没有找到任何漏洞).干得好,尼克.

Follow up: I have been using this answer below by Nick for over 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 没有正确包装单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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