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

前端 未结 7 1273
心在旅途
心在旅途 2020-12-01 12:19

This is the code causing the warning:

private override func layoutAttributesForItemAtIndexPath(indexPath: NSIndexPath) -> UICollectionViewLayoutAttributes         


        
7条回答
  •  忘掉有多难
    2020-12-01 12:36

    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!

提交回复
热议问题