ios - 使用我自己的 UICollectionViewFlowLayout 子类滚动时,UICollectionView 项目消失

标签 ios objective-c iphone swift

上下文:我正在使用 UICollectionView 进行照片查看。每张图片都是一个包含一个 UIImage 的单元格。图片可以有不同的大小,我希望它们能填满整个屏幕。 所以我编写了一个类来确定每个 UICollectionCell 的框架,并让 UICollectionViewFlowLayout 的子类向该类请求每个项目的正确框架。

我对 UICollectionViewFlowLayoutClass 的实现:

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
}

项目的数量不是静态的,它会随着到达 View 的末尾而增长。所以这里发生的是: manager确定最后一个item的Origin.y + Size.Height(+10点确定),width就是UICollectionView的宽度。

仍然所有单元格的帧都在该方法返回的尺寸范围内。但仍有一些细胞消失或根本不出现。

当我进一步滚动 UICollectionView 时,我看到其他单元格位于正确的位置。所以我的观点存在差距,但流程仍在继续。 (当出现间隙时,不会调用 collectionViewDelegate 中缺失索引路径中的项目。例如,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
}

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

最佳答案

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

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

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

所以如果没有这个属性设置, View 会表现得很奇怪,我仍然不知道原因,但我很高兴它能工作。 (如果有人知道原因,我想听听)

关于ios - 使用我自己的 UICollectionViewFlowLayout 子类滚动时,UICollectionView 项目消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29779292/

相关文章:

ios - 将变量从 UIViewController 传递到自定义 UITableViewCell

ios - 像 UITabBarItem 一样在 UIBarButtonItem 下添加标签

iphone - 图像质量较低不像在相册中显示

ios - 压缩 UIImage 但保持大小

objective-c - 如何让 Scripting Bridge 在运行时生成 Objective-C 类?

objective-c - 暂时禁用热角和停靠屏幕切换

ios - 如何更改iPhone键盘返回键的颜色?

ios - 当表格 View 向上滚动时,表格 View 第一部分(第 0 部分)没有向上移动

iphone - 将文本设置为自定义表格单元格标签时崩溃

iphone - 在单元测试期间,cellForRowAtIndexPath为nil