在我們的控制臺中會報這樣的錯誤:
This is likely occurring because the flow layout subclass XXX(類名) is modifying attributes returned by UICollectionViewFlowLayout without copying them
先上圖吧!
控制臺報錯.png
看到這個錯誤真心很不舒服,作為有強迫癥的程序員來說就該去想辦法解決這樣問題了,我查閱資料找到了出錯原因如下:
override func layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
let array = super.layoutAttributesForElementsInRect(rect)!
return array
}
自定義CollectionView報錯原因.png
解決方案如下:
override func layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
let array = NSArray(array: super.layoutAttributesForElementsInRect(rect)!, copyItems: true) as! [UICollectionViewLayoutAttributes]
return array
}
現在再運行你的程序 看看是不是很干凈了呢 ?
喜歡就點一個一下喜歡吧 謝謝!