[問題] CollectionView結合CoreData

看板MacDev作者 (USA)時間8年前 (2016/05/26 13:39), 8年前編輯推噓1(103)
留言4則, 2人參與, 最新討論串1/1
我是初學者,想請教一個讓我卡很久的問題,官方文件和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

05/26 19:00, , 1F
丟git吧 超難閱讀的..
05/26 19:00, 1F

05/26 19:49, , 2F
collectionView的資料更新方式記得是先修改model
05/26 19:49, 2F

05/26 19:50, , 3F
改完之後再呼叫performBatchUpdates 把要新增刪除的call
05/26 19:50, 3F

05/26 19:50, , 4F
放在裡面
05/26 19:50, 4F
文章代碼(AID): #1NHemGib (MacDev)