我是初学者,想请教一个让我卡很久的问题,官方文件和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的问题?
烦请请各位帮忙,如果有要我更新什么资讯我会尽快回复,感谢各位
手机排版不好意思有点乱