[問題] CollectionView結合CoreData

作者: z82101096 (USA)   2016-05-26 13:39:26
我是初學者,想請教一個讓我卡很久的問題,官方文件和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的問題?
煩請請各位幫忙,如果有要我更新什麼資訊我會盡快回覆,感謝各位
手機排版不好意思有點亂
作者: tentenlee (天天)   2016-05-26 19:00:00
丟git吧 超難閱讀的..
作者: Esvent (Esvent)   2016-05-26 19:49:00
collectionView的資料更新方式記得是先修改model改完之後再呼叫performBatchUpdates 把要新增刪除的call放在裡面

Links booklink

Contact Us: admin [ a t ] ucptt.com