9

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

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.

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

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.

Tejas Ardeshna
  • 4,343
  • 2
  • 20
  • 39
potato
  • 4,479
  • 7
  • 42
  • 99
  • Did this only start happening with Xcode 7? Something very similar has been happening to me as well since updating to Xcode 7 (and installing iOS9). I'm Obj-C, not swift, but the error is telling you you're not copying the attributes before changing them. I tried doing something like `NSArray *allAttributesInArray = [[super layoutAttributesForElementsInRect:rect] copy];` before manipulating them, but that doesn't seem to work either. – wanderingme Sep 21 '15 at 23:55
  • i never used collection view before xcode 7. I ended up using a plain old flowLayout without subclassing so I cant really provide an answer – potato Sep 22 '15 at 07:10
  • @wanderingme have you found out how to solve this? I have got the same one and nothing has helped to solve it yet. – Solomiya Sep 30 '15 at 19:08
  • I did, but for Obj-C and using a custom subclass called TLLayoutTransitioning. I mentioned my workaround here: https://github.com/wtmoose/TLLayoutTransitioning/issues/26 – wanderingme Oct 01 '15 at 19:54
  • TL;DR version: I reloaded the UICollectionView data before transitioning between my two UICollectionViewFlowLayouts. – wanderingme Oct 01 '15 at 19:58
  • This is potentially a duplicate of: http://stackoverflow.com/q/31508153/1470581 – Derek Lee Nov 04 '15 at 06:56

1 Answers1

11

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:

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
    } 
joern
  • 27,354
  • 7
  • 90
  • 105