使用我自己的UICollectionViewFlowLayout子类滚动时,UICollectionView项目会消失 [英] UICollectionView items disappear when scrolling with my own UICollectionViewFlowLayout subclass

查看:134
本文介绍了使用我自己的UICollectionViewFlowLayout子类滚动时,UICollectionView项目会消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上下文:我使用 UICollectionView 作为photoview。每张图片都是一张 UIImage 的单元格。图像可以有不同的大小,我希望它们填满整个屏幕。
所以我编写了一个类来确定每个 UICollectionCell 的框架,并让 UICollectionViewFlowLayout 的子类询问每个项目的右边框架的类。

Context: I am using an UICollectionView for a photoview. Every picture is a cell with one UIImage. Images can have different sizes and I want them to fill the whole screen. So I wrote a class who determines the frame of every single UICollectionCell and let a subclass of UICollectionViewFlowLayout ask that class for the right frame for every item.

我的 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。

So the frame asks my MazeManager to give back a frame. The returned frames seem to be correct and they all fit in the UICollectionView.

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

When I open my app everything looks fine, even when I scroll. But when I scroll to a specific position (this position feels random because it depends on the images I test with, but with the same set of images the positions are the same) cells disappear from my view. When I scroll back they return.

我已经检查过哪些单元格没有被隐藏,但它们从未被隐藏过。

I've checked if the cells where not hidden, but they never are.

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

On some other threads with similar issues the answer is to implement the collectionViewContentSize so I did:

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的宽度。

The number of items is not static and it grows while reaching the end of the view. So what happens here is: The manager determines the Origin.y + the Size.Height of the last item (+ 10 points to be sure), the width is the width of the UICollectionView.

单元格的所有帧都在此方法返回的大小范围内。但仍有一些单元格消失或根本不会出现。

Still all the frames of the cells are within the sizes returned by this method. But still some cell disappear or never appear at all.

当我进一步滚动UICollectionView时,我看到位于正确位置的其他单元格。所以我的观点存在差距,但流程还在继续。 (当出现间隙时,collectionViewDelegate中缺少idex路径的项目没有调用。例如,CollectionView请求项目:1,2,3,4,5,6,12,13,14)。

When I scroll further through the UICollectionView I see other cells which are positioned on the right place. So there are gaps in my view, but the flow continues. (When a gap appears there are no calls for the items at the missing idexpaths in the collectionViewDelegate. For example the CollectionView asks for items: 1,2,3,4,5,6, 12,13,14).

控制台没有打印任何关于错误定位的信息,我已经检查过,一切都在ContentSize中。所以我几乎没有选择。有人可以解释一下我的情况吗?

The console prints nothing about wrong positioning, I've checked and everything is within the ContentSize. So I'm almost out of options. Can anybody explain what's happening in my case?

谢谢。

编辑:

在我寻找解决方案时,我已经找到了上述帖子( UICollectionView的细胞消失)我已经完成了4个步骤中的3个步骤。

While I was looking for a solution, I already found the mentioned post (UICollectionView's cell disappearing) and I already did 3 of the 4 steps.

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

我刚刚添加了滚动方向初始值设定项:

And I just added the scroll direction in the initializer:

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

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

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

Unfortunately this doesn't fix my issue, so it doesn't seem a duplicate to me.

推荐答案

我不知道为什么,但是当我在 init 方法:

I don't know why, but it works when I add the following line in my init methods:

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

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

165 is the average height for my cells (I need to make this less static). The size I specified here seems to be ignored, because the sizes I see on my screen are all calculated in the layoutAttributesForItemAtIndexPath.

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

So without this property set the view behaves strange, I still don't know the reason, but I'm glad it works. (If anyone knows the reason, I would like to hear it)

这篇关于使用我自己的UICollectionViewFlowLayout子类滚动时,UICollectionView项目会消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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