ios - 使用子类 collectionViewFlowLayout 时出现奇怪的错误

标签 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: 方法删除 Collection View 中的项目时,调试器会抛出此错误消息。删除实际上成功了并且完全正常工作,但我对这个调试器输出有点困惑。有人可以解释我应该做什么来取悦调试器吗?我真的不明白应该添加什么代码和在哪里。

//错误信息

2015-08-02 12:39:42.208 nameOfMyProject[1888:51831] Logging only once 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] This is likely occurring because the flow layout subclass nameOfMyProject.ShopLayout is modifying attributes returned by UICollectionViewFlowLayout without copying them

2015-08-02 12:39:42.209 nameOfMyProject[1888:51831] Snapshotting a view that has not been rendered results in an empty snapshot. Ensure your view has been rendered at least once before snapshotting or snapshot after screen updates.

最佳答案

错误发生是因为您在操作属性时没有先复制它。所以这应该修复错误:

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
    } 

关于ios - 使用子类 collectionViewFlowLayout 时出现奇怪的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31771089/

相关文章:

ios - 如何在 iOS 上录制对话/电话?

xcode - UIView内的UICollectionView

swift - 如何创建之间没有空格的正方形网格?

swift - 如何让 IGListKit 在水平滚动的 UICollectionView 中分隔项目?

iphone - 如何使用 Mapkit 在 iPhone 中显示离线 map

ios - 传播特定的触摸事件以在下方查看

ios - 是否可以使用 EarlGrey(iOS UI 测试)关闭系统警报?

ios - 如何在一个 View Controller 中拥有多个 Collection View ?

ios6 - layoutAttributesForItemAtIndexPath - 何时调用?

ios - 在 UICollectionView 中显示图像 - 如何实现图像之间的固定垂直间距?