【问题标题】:Strange disappearing items when scrolls滚动时奇怪的消失项目
【发布时间】:2016-01-19 13:15:04
【问题描述】:

预设,

我有 collectionViewFlowLayout 的子类

- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds {
    return YES;
   }

- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect {
    NSArray *arr = [super layoutAttributesForElementsInRect:rect];
    BBLog(@"ARRA:%@", arr);
    for (UICollectionViewLayoutAttributes *attr in arr) {
        if (CGAffineTransformIsIdentity(attr.transform)) {
            attr.transform = CGAffineTransformMakeRotation((CGFloat)M_PI);
        }
    }

    return arr;
}

CollectionView 旋转到上下滚动

 self.collectionView.transform = CGAffineTransformMakeRotation((CGFloat)M_PI);

但是即使只使用原生collectionViewFlowLayout 没有子类化,一个git这个错误

问题

我在聊天中有两条消息和更多消息,但是当滚动到底部(通常是顶部)时,第二项消失了。

给定rect的layoutAttributesForElementsInRect返回两个indexPaths 0-0和0-1的两个属性,但是委托方法

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath

只为 indexPath 0-0 调用

这里的图片

更新 所以我找到了为什么会发生 - 这行代码

attr.transform = CGAffineTransformMakeRotation((CGFloat)M_PI);

看看是否移除变换

【问题讨论】:

    标签: ios uicollectionview uicollectionviewcell uicollectionviewlayout uicollectionviewdelegate


    【解决方案1】:

    我不完全确定,但我认为,当你继承 UICollectionViewFlowLayout 时,你不应该直接修改属性,而是复制属性,修改它并返回它。

    顶部语句的简短解释: 您必须继承 UICollectionViewFlowDelegate ( UICollectionViewDelegateFlowLayout 的父级),然后创建自己的属性并根据需要修改它们,但这需要实现更多的自定义逻辑。

    同时查看您是否在控制台中收到任何错误或警告。

    看看这个问题:Warning: UICollectionViewFlowLayout has cached frame mismatch for index path 'abc'

    希望我至少能提供一点帮助。

    【讨论】:

      【解决方案2】:

      对不起,所有,但我找到了原因并解决了问题。

      这只发生在设备上而不是模拟器上

      看三个CGRect

      iPhone 5S

      (CGRect) oldFrame = (origin = (x = -0.000000000000056843418860808015, y = 0), size = (width = 320.00000000000006, height = 314.00000000000006))
      

      iPhone 5C

      (CGRect) oldFrame = (origin = (x = 0, y = 0), size = (width = 320, height = 314))

      模拟器英特尔酷睿

      (CGRect) oldFrame = (origin = (x = 0, y = 0), size = (width = 320, height = 314))
      

      我对他们所有人应用旋转变换

      CGAffineTransformMakeRotation((CGFloat)M_PI)
      

      首先是在 iPhone 5S ARM Apple A7 CPU

      其次是在 iPhone 5C ARM Apple A6 CPU

      第三,它在英特尔酷睿 iX 处理器的模拟器上。

      所以,我的解决方法是:

      - (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {
          UICollectionViewLayoutAttributes *retAttr = [super layoutAttributesForItemAtIndexPath:indexPath];
      
          if (CGAffineTransformIsIdentity(retAttr.transform)) {
              retAttr.transform = CGAffineTransformMakeRotation((CGFloat)M_PI);
          }
      
          CGRect oldFrame = retAttr.frame;
      
          oldFrame.origin.x = retAttr.frame.origin.x > 0 ?: 0;
      
          retAttr.frame = oldFrame;
      
          return retAttr;
      }
      

      【讨论】:

      • 有什么快速解决方案?
      • 在 swift 中没有测试,现在只在 objc 中编码。但我认为同样的问题。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-10
      相关资源
      最近更新 更多