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

时间:2015-08-02 10:48:19

标签: ios uicollectionview uicollectionviewlayout collectionview

我创建了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:方法删除集合视图中的项目时,调试器会抛出此错误消息。删除实际上成功并且完全正常工作,但我对此调试器输出有点困惑。有人可以解释我应该做什么来取悦调试器吗?我不太了解代码和应该添加的位置。

// ERROR MESSAGE

  

2015-08-02 12:39:42.208 nameOfMyProject [1888:51831]仅记录一次   对于UICollectionViewFlowLayout缓存不匹配的框架2015-08-02   12:39:42.209 nameOfMyProject [1888:51831] UICollectionViewFlowLayout   已缓存索引路径{length = 2,path = 0 - 11}的帧不匹配 - 缓存值:   {{106.13333333333333,131.13333333333333},{75.733333333333348,   75.733333333333348}};预期价值:{{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]快照一个   尚未呈现的视图导致空快照。确保   在快照或之前,您的视图至少呈现过一次   屏幕更新后的快照。

1 个答案:

答案 0 :(得分:9)

发生错误是因为您正在操作属性而不先复制它。所以这应该可以解决错误:

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
    }