我做了一个 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
}
当我使用 performBatchUpdates
: 方法删除 Collection View 中的项目时,调试器会抛出此错误消息。删除实际上成功了并且完全正常工作,但我对这个调试器输出有点困惑。有人可以解释我应该做什么来取悦调试器吗?我真的不明白应该添加什么代码和在哪里。
//错误信息
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.
最佳答案
错误发生是因为您在操作属性时没有先复制它。所以这应该修复错误:
override func finalLayoutAttributesForDisappearingItemAtIndexPath(itemIndexPath: NSIndexPath) -> UICollectionViewLayoutAttributes? {
let attr = self.layoutAttributesForItemAtIndexPath(itemIndexPath)?.copy() as! UICollectionViewLayoutAttributes
// manipulate the attr
return attr
}
当您在 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
}
关于ios - 使用子类 collectionViewFlowLayout 时出现奇怪的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31771089/