当使用子类化的collectionViewFlowLayout时,我遇到了奇怪的错误。

9

我创建了一个collectionViewFlowLayout的子类,然后实现了以下代码:

override func finalLayoutAttributesForDisappearingItemAtIndexPath(itemIndexPath: NSIndexPath) -> UICollectionViewLayoutAttributes? {
        let attr = self.layoutAttributesForItemAtIndexPath(itemIndexPath)
        attr?.transform = CGAffineTransformRotate(CGAffineTransformMakeScale(0.8, 0.8), CGFloat(M_PI))
        attr?.center = CGPointMake(CGRectGetMidX(self.collectionView!.bounds), CGRectGetMidY(self.collectionView!.bounds))
        return attr
    }

当我使用performBatchUpdates方法在集合视图中删除项目时,调试器会抛出此错误消息。实际上,删除成功并且完全工作,但是我对这个调试器输出有点困惑。请问有人可以解释一下我应该做些什么来满足调试器的要求吗?我不太理解应该添加什么代码以及在哪里添加。
//错误消息
2015-08-02 12:39:42.208 nameOfMyProject[1888:51831] 只记录一次 for UICollectionViewFlowLayout cache mismatched frame 2015-08-02 12:39:42.209 nameOfMyProject[1888:51831] UICollectionViewFlowLayout has cached frame mismatch for index path {length = 2, path = 0 - 11} - cached value: {{106.13333333333333, 131.13333333333333}, {75.733333333333348, 75.733333333333348}}; expected value: {{192.5, 288}, {94.666666666666671, 94.666666666666671}}
2015-08-02 12:39:42.209 nameOfMyProject[1888:51831] 这可能是因为流布局子类nameOfMyProject.ShopLayout正在修改UICollectionViewFlowLayout返回的属性而没有复制它们
2015-08-02 12:39:42.209 nameOfMyProject[1888:51831] 快照一个未被渲染的视图将导致一个空的快照。确保您的视图至少被渲染一次,然后再进行快照或在屏幕更新后进行快照。

这种情况是从Xcode 7开始出现的吗?自从我更新到Xcode 7(并安装了iOS9)以来,我也遇到了非常类似的问题。我使用的是Obj-C而不是Swift,但错误提示你在更改属性之前没有复制它们。我尝试过在操作之前做类似于NSArray *allAttributesInArray = [[super layoutAttributesForElementsInRect:rect] copy];的事情,但那似乎也不起作用。 - wanderingme
我以前从未使用过Xcode 7的CollectionView。最终,我使用了一个普通的flowLayout而没有子类化,所以我无法提供答案。 - potato
@wanderingme 你解决这个问题了吗?我也遇到了同样的问题,但是还没有找到解决方法。 - Solomiya
我做了,但是使用的是Obj-C和一个名为TLLayoutTransitioning的自定义子类。我在这里提到了我的解决方法:https://github.com/wtmoose/TLLayoutTransitioning/issues/26 - wanderingme
简短版:在我的两个UICollectionViewFlowLayout之间转换之前,我重新加载了UICollectionView数据。 - wanderingme
这可能是重复的内容:https://dev59.com/4lwZ5IYBdhLWcg3wYvgk - Derek Lee
1个回答

11

出现错误是因为您在没有先复制属性的情况下对其进行操作。所以这应该可以解决错误:

override func finalLayoutAttributesForDisappearingItemAtIndexPath(itemIndexPath: NSIndexPath) -> UICollectionViewLayoutAttributes? {
    let attr = self.layoutAttributesForItemAtIndexPath(itemIndexPath)?.copy() as! UICollectionViewLayoutAttributes
    // manipulate the attr
    return attr
}

当你在layoutAttributesForElementsInRect(rect: CGRect)中遇到相同的错误时,你需要复制数组中的每个项,而不仅仅是复制整个数组:

override func layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
        let attributes = super.layoutAttributesForElementsInRect(rect)
        var attributesCopy = [UICollectionViewLayoutAttributes]()
        for itemAttributes in attributes! {
            let itemAttributesCopy = itemAttributes.copy() as! UICollectionViewLayoutAttributes
            // manipulate itemAttributesCopy
            attributesCopy.append(itemAttributesCopy)
        }
        return attributesCopy
    } 

复制layoutAttributesForElementsInRect中的所有属性可以解决警告。 - Rool Paap
@Ranjit 你看过上面评论中提到的 Github 问题了吗?(https://github.com/wtmoose/TLLayoutTransitioning/issues/26) - joern
@joern,正在看它。 - Ranjit

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接