Warning: UICollectionViewFlowLayout has cached frame mismatch for index path 'abc'
Asked Answered
P

7

36

This is the code causing the warning:

private override func layoutAttributesForItemAtIndexPath(indexPath: NSIndexPath) -> UICollectionViewLayoutAttributes? {
    let attributes = super.layoutAttributesForItemAtIndexPath(indexPath)
    let distance = CGRectGetMidX(attributes!.frame) - self.midX;
    var transform = CATransform3DIdentity;
    transform = CATransform3DTranslate(transform, -distance, 0, -self.width);
    attributes!.transform3D = CATransform3DIdentity;
    return attributes
}

The console also prints:

This is likely occurring because the flow layout "xyz" is modifying attributes returned by UICollectionViewFlowLayout without copying them.

How do I fix this warning?

Phelloderm answered 20/7, 2015 at 2:42 Comment(0)
C
52

This is likely occurring because the flow layout "xyz" is modifying attributes returned by UICollectionViewFlowLayout without copying them

And sure enough, that's just what you are doing:

private override func layoutAttributesForItemAtIndexPath(indexPath: NSIndexPath) -> UICollectionViewLayoutAttributes? {
    let attributes = super.layoutAttributesForItemAtIndexPath(indexPath)
    let distance = CGRectGetMidX(attributes!.frame) - self.midX;
    var transform = CATransform3DIdentity;
    transform = CATransform3DTranslate(transform, -distance, 0, -self.width);
    attributes!.transform3D = CATransform3DIdentity;
    return attributes
}

I expect that if you simply say:

let attributes = 
    super.layoutAttributesForItemAtIndexPath(indexPath).copy() 
    as! UICollectionViewLayoutAttributes

or similar, the problem will go away.

Cymar answered 20/7, 2015 at 2:57 Comment(8)
In Swift 2, you can't call copy() on super.layoutAttributesForItemAtIndexPath. However you can call it on the individual layout attributes, then put those into an array and return that instead. See https://mcmap.net/q/145845/-warning-uicollectionviewflowlayout-has-cached-frame-mismatch-for-index-path-39-abc-39Langsyne
@TrevorAlyn yes you can. Here is an example of me doing it. github.com/mattneub/Programming-iOS-Book-Examples/blob/… and the code I give in my answer is Swift 2 and works fine.Cymar
@matt: Oops -- I was trying to call copy() on layoutAttributesForElementsInRect, not layoutAttributesForItemAtIndexPath. But Xcode gives me a "has no member copy" error when I try to do this: var attributes = super.layoutAttributesForElementsInRect(rect).copy()Langsyne
@TrevorAlyn If you don't copy it, is it giving you the same error?Cymar
Nope. Fortunately I can copy the individual attributes, like so: attributesCopy.append(attributes![0].copy() as! UICollectionViewLayoutAttributes)Langsyne
@TrevorAlyn Right but if you're not having a problem I don't see why you need to make a copy.Cymar
@matt: I'm copying them so I can make adjustments to them.Langsyne
In Swift4.1 I used override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? { var array: [UICollectionViewLayoutAttributes] = [] for object in super.layoutAttributesForElements(in: rect)! { array.append(object.copy() as! UICollectionViewLayoutAttributes) }Enphytotic
G
43

In addition to the great answer above.

I know the example code is written in swift, but I thought that it can be helpful to have the Objective-C version.

For Objective-C, this won't work, because the copy function does only a shallow copy. You will have to do this:

NSArray * original   = [super layoutAttributesForElementsInRect:rect];
NSArray * attributes = [[NSArray alloc] initWithArray:original copyItems:YES];

I have added a temp variable for readability.

Gilda answered 16/10, 2015 at 13:49 Comment(1)
In addition : my answer is handy when You use an array of attributes, if You want to get only the attributes for a specific indexPath see the answer of Ayman IbrahimGilda
H
23

I had this issue when overriding layoutAttributesForElementsInRect. Iterating through each element in the super.layoutAttributesForElementsInRect(rect) array and calling copy wasn't working for me, so I ended up falling back on Foundation classes and using NSArray's copyItems:

override func layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
    // unwrap super's attributes
    guard let superArray = super.layoutAttributesForElementsInRect(rect) else { return nil }

    // copy items
    guard let attributes = NSArray(array: superArray, copyItems: true) as? [UICollectionViewLayoutAttributes] else { return nil }

    // modify attributes

    return attributes
}
Harriettharrietta answered 30/3, 2016 at 17:24 Comment(4)
Thank you for posting this even though there was another answer. I was iterating through each element in an array and copying as well. Wasn't working for me either. So thanks for posting this solution.Tarter
@JAL, how to append attributes to it. because its a NSArrayCooe
@Cooe cast the NSArray as a Swift Array like I do with the attributes variableHarriettharrietta
@JAL, I solved the copy issue, but I am facing one more issue, I have created a SO question here #38147413Cooe
G
10

It's not the answer to original question, but can help for layoutAttributesForElements(in rect: CGRect) (Swift 3.0):

let safeAttributes = super.layoutAttributesForElements(in: rect)?.map { $0.copy() as! UICollectionViewLayoutAttributes }
safeAttributes?.forEach { /* do something with attributes*/ }
Gambado answered 29/3, 2017 at 23:44 Comment(0)
O
6

Adding to @Georgi answer

<NSCopying> must be conformed and add copy message call to layoutAttributesForItemAtIndexPath

UICollectionViewLayoutAttributes* attributes = [[super layoutAttributesForItemAtIndexPath:indexPath] copy];
Odericus answered 21/10, 2015 at 21:11 Comment(0)
F
3

Updated response for Swift 3!

for func layoutAttributesForElements

override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {

    guard let attributes = super.layoutAttributesForElements(in: rect) else {
        return nil
    }

    guard let attributesToReturn =  attributes.map( { $0.copy() }) as? [UICollectionViewLayoutAttributes] else {
        return nil
    }

    return attributesToReturn

}

for func layoutAttributesForItem

override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {

    guard let currentItemAttributes = super.layoutAttributesForItem(at: indexPath)?.copy() as? UICollectionViewLayoutAttributes else {
        return nil
    }

    return currentItemAttributes
}

If you override both function, you have to call copy on both function!

Good coding!

Flushing answered 19/7, 2017 at 10:59 Comment(0)
R
0

I have subclassed UICollectionViewFlowLayout. Inside layoutAttributesForElementsInRect() I did this change:

change from

guard let attributesForItem: UICollectionViewLayoutAttributes = self.layoutAttributesForItemAtIndexPath(indexPath) else {
    return
}

change to

guard let attributesForItem = self.layoutAttributesForItemAtIndexPath(indexPath)?.copy() as? UICollectionViewLayoutAttributes else {
    return
}
Russellrusset answered 8/6, 2016 at 7:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.