看板 MacDev 關於我們 聯絡資訊
我是初學者,想請教一個讓我卡很久的問題,官方文件和Stackoverflow都查了很多還是 沒有解出來,關於UICollectionView和CoreData的問題,使用Swift 我想要新增一個CollectionViewCell在CollectionViewController裡,使用CoreData進行 儲存,而如果是使用TableView會呼叫controllerWillChangeContent和controllerDidCha ngeContent,但是CollectionView沒有這兩個函式,是使用performBatchUpdates:comple tion,但是我不太清楚要怎麼寫這裡的函式,是要重寫一次insert、delete嗎? 這裡是相關程式碼 func controller(controller: NSFetchedResultsController, didChangeObject anObje ct: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetc hedResultsChangeType, newIndexPath: NSIndexPath?) { switch type { case .Insert: if let _newIndexPath = newIndexPath { cubeCollectionView?.insertItemsAtIndexPaths([_newIndexPath]) } case .Delete: if let _indexPath = indexPath { cubeCollectionView?.deleteItemsAtIndexPaths([_indexPath]) } case .Update: if let _indexPath = indexPath { cubeCollectionView?.reloadItemsAtIndexPaths([_indexPath]) } default: cubeCollectionView?.reloadData() } cubes = controller.fetchedObjects as! [Cube] } 這裡不清楚怎麼寫 func controllerDidChangeContent(controller: NSFetchedResultsController) { cubeCollectionView?.performBatchUpdates({ self.cubeCollectionView?.reloadData() } , completion: nil) } 執行後他丟出了 This is usually a bug within an observer of NSManagedObjectContextObjectsDidCh angeNotification. attempt to insert item 0 into section 0, but there are only 0 items in section 0 after the update with userInfo (null) 似乎是更新item的問題? 煩請請各位幫忙,如果有要我更新什麼資訊我會盡快回覆,感謝各位 手機排版不好意思有點亂 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 203.187.43.7 ※ 文章網址: https://www.ptt.cc/bbs/MacDev/M.1464241168.A.B25.html ※ 編輯: z82101096 (203.187.43.7), 05/26/2016 13:40:48
tentenlee: 丟git吧 超難閱讀的.. 05/26 19:00
Esvent: collectionView的資料更新方式記得是先修改model 05/26 19:49
Esvent: 改完之後再呼叫performBatchUpdates 把要新增刪除的call 05/26 19:50
Esvent: 放在裡面 05/26 19:50