is modifying attributes returned by UICollectionViewFlowLayout without copying them--Swift在自定義collectionview的flowlayout的時候控制臺報錯解決方

在我們的控制臺中會報這樣的錯誤:

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
  }

現在再運行你的程序 看看是不是很干凈了呢 ?
喜歡就點一個一下喜歡吧 謝謝!

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容