關於collectionView.performBatchUpdates()方法


 

   今天想實現一個簡單的collectionView動畫效果,查閱相關資料發現,實現 collectionView.performBatchUpdates()方法即可,於是掉坑里了。

    文檔: public func performBatchUpdates(updates: (() -> Void)?, completion: ((Bool) -> Void)?) // allows multiple insert/delete/reload/move calls to be animated simultaneously. Nestable.

    根據文檔的意思,我這邊是做reload操作,發現剛好符合我的需求,於是擼起

  self.collectionView.performBatchUpdates({ () -> Void in

           self.collectionView.reloadData()

        }) { (finished) -> Void in

        }

    話說,一共就幾行代碼,so easy,我怎么沒可能會錯呢。。結果是:

 Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of items in section 0.  The number of items contained in an existing section after the update (9) must be equal to the number of items contained in that section before the update (8), plus or minus the number of items inserted or deleted from that section (0 inserted, 0 deleted) and plus or minus the number of items moved into or out of that section (0 moved in, 0 moved out)

     當時就不淡定了。。。怎么可能,哼。。。於是乎啊,我又運行幾次,結果都是這么殘忍。唉。。。都是淚啊,看看了文檔,發現,另一個方法

      public func reloadSections(sections: NSIndexSet)  哈哈,難道在這個block中不能用reloadData(),於是我都試了試reloadSectionS(),binggo,搞定

     思索再三,發現原因可能是: 我在點擊出發批量數據處理的時候,沒有只能具體要操作的內容,所以報錯了。。。。嗚嗚。。。可憐啊

   

  另外tableView也有相應的簡單效果

  beginUpdates() / endUpdates() 在此之間寫響應的代碼就行了. 官方文檔上寫的很明白,沒事還是多看看文檔吧。。哈哈。。。

效果圖:

   

點擊之后: 

     

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM