一尘不染

警告:UICollectionViewFlowLayout已为索引路径“ abc”缓存帧不匹配

swift

这是引起警告的代码:

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
}

控制台还会打印:

这很可能发生,因为流布局“ xyz”正在修改UICollectionViewFlowLayout返回的属性而不复制它们。

如何解决此警告?


阅读 223

收藏
2020-07-07

共1个答案

一尘不染

这很可能发生,因为流布局“ xyz”正在修改UICollectionViewFlowLayout返回的属性而不复制它们

确实,这就是您正在做的事情:

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
}

我希望您能简单地说:

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

或类似的问题将消失。

2020-07-07