[问题] 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