【问题标题】:UICollectionView items disappear when scrolling with my own UICollectionViewFlowLayout subclass使用我自己的 UICollectionViewFlowLayout 子类滚动时,UICollectionView 项目消失
【发布时间】:2015-04-21 17:32:48
【问题描述】:

上下文:我正在使用UICollectionView 进行照片浏览。每张图片都是一个带有UIImage 的单元格。图像可以有不同的大小,我希望它们填满整个屏幕。 所以我写了一个类来确定每个UICollectionCell 的框架,并让UICollectionViewFlowLayout 的子类要求该类为每个项目提供正确的框架。

我对@9​​87654327@的实现

override func layoutAttributesForElementsInRect(rect: CGRect) -> [AnyObject]? {
    let attributesToReturn = super.layoutAttributesForElementsInRect(rect) as? [UICollectionViewLayoutAttributes]
    for attributes in attributesToReturn ?? [] {
        if attributes.representedElementCategory == .Cell {
            let frame = self.layoutAttributesForItemAtIndexPath(attributes.indexPath).frame
            attributes.size = frame.size
            attributes.frame = frame
        }
    }
    return attributesToReturn
}

override func layoutAttributesForItemAtIndexPath(indexPath: NSIndexPath) -> UICollectionViewLayoutAttributes! {
    let curAttributes = super.layoutAttributesForItemAtIndexPath(indexPath)
    let frame = mazeManager.sizeForItemAtIndex(indexPath, collectionView: collectionView!)
    curAttributes.size = frame.size
    curAttributes.frame = frame
    return curAttributes
}

所以框架要求我的 MazeManager 归还一个框架。返回的帧似乎是正确的,它们都适合 UICollectionView。

当我打开我的应用程序时,一切看起来都很好,即使我滚动时也是如此。但是当我滚动到特定位置时(这个位置感觉很随机,因为它取决于我测试的图像,但是对于同一组图像,位置是相同的)单元格从我的视图中消失。当我向后滚动时,它们会返回。

我检查了单元格是否没有隐藏,但它们从来没有。

在其他一些有类似问题的线程上,答案是实现collectionViewContentSize,所以我这样做了:

override func collectionViewContentSize() -> CGSize {
    let size = mazeManager.collectionViewContentSize
    return size.height < collectionView!.frame.size.height ? collectionView!.frame.size : size
}

项目的数量不是静态的,它会随着到达视图的末端而增长。所以这里发生的是: 管理器确定 Origin.y + 最后一项的 Size.Height(+10 分肯定),宽度就是 UICollectionView 的宽度。

单元格的所有框架仍然在此方法返回的大小范围内。但仍有一些细胞消失或根本不出现。

当我进一步滚动 UICollectionView 时,我看到其他单元格位于正确的位置。因此,我认为存在差距,但流程仍在继续。 (当出现间隙时,不会调用 collectionViewDelegate 中缺少的 idexpath 处的项目。例如,CollectionView 要求项目:1、2、3、4、5、6、12、13、14)。

控制台没有打印任何关于错误定位的信息,我已经检查过了,所有内容都在 ContentSize 范围内。所以我几乎没有选择。谁能解释一下我的情况?

谢谢。

编辑:

在寻找解决方案时,我已经找到了提到的帖子 (UICollectionView's cell disappearing),并且我已经完成了 4 个步骤中的 3 个。

override func shouldInvalidateLayoutForBoundsChange(newBounds: CGRect) -> Bool {
    return true
}

我只是在初始化程序中添加了滚动方向:

override init(){
    super.init()
    scrollDirection = .Vertical
}

required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    self.scrollDirection = .Vertical
}

不幸的是,这并不能解决我的问题,所以对我来说似乎不是重复的。

【问题讨论】:

  • @angryTurtle 我不认为这是重复的,所以我通过上述主题的尝试扩展了我的问题。
  • 酷,这就是为什么我添加了评论并且没有将其标记为审核

标签: ios objective-c iphone swift


【解决方案1】:

我不知道为什么,但是当我在我的 init 方法中添加以下行时它可以工作:

    self.itemSize = CGSize(width: 165,height: 165)

165 是我的单元格的平均高度(我需要减少它的静态)。我这里指定的大小似乎被忽略了,因为我在屏幕上看到的大小都是在layoutAttributesForItemAtIndexPath 中计算的。

因此,如果没有设置此属性,视图的行为会很奇怪,我仍然不知道原因,但我很高兴它可以工作。 (如果有人知道原因,我想听听)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多