当使用子类的collectionViewFlowLayout时,我将得到一个奇怪的错误。

时间:2022-11-20 21:51:22

I made a subclass of collectionViewFlowLayout. After that, I implemented the following code:

我做了一个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
    }

When I delete items in collection view using performBatchUpdates: method the debugger throws this error message. The deletion actually succeeds and is fully working, but I am a little confused about this debugger output. Can someone please explain should I do to please debugger? I don't really understand what code and where should be added.

当我在收集视图中使用performBatchUpdates删除项目时:调试器会抛出这个错误消息。删除实际上是成功的,并且是完全工作的,但是我对这个调试器输出有点困惑。有人能解释一下我该怎么做吗?我真的不明白什么代码和应该添加什么。

//ERROR MESSAGE

/ /错误信息

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}}

mso - ascii - font - family: tahoma; mso - fareast - font - family:宋体;mso - bidi - font - family: " times new roman "; mso - bidi - theme - font: minor - bidi ' > < span预期值:{{192.5,288},{94.6666666666671,94.6666666666671}

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

这可能是由于流布局子类nameOfMyProject的原因。ShopLayout是修改UICollectionViewFlowLayout返回的属性,而不需要复制它们。

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.

[参考译文]一个没有在空快照中呈现结果的视图。确保您的视图在快照或屏幕更新后至少呈现一次。

1 个解决方案

#1


9  

The error occurs because you are manipulating the attribute without copying it first. So this should fix the error:

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

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

When you come across the same error in layoutAttributesForElementsInRect(rect: CGRect) you have to copy each of the items in the array instead of just copying the array:

当您在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
    } 

#1


9  

The error occurs because you are manipulating the attribute without copying it first. So this should fix the error:

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

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

When you come across the same error in layoutAttributesForElementsInRect(rect: CGRect) you have to copy each of the items in the array instead of just copying the array:

当您在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
    }