作者z82101096 (USA)
看板MacDev
标题[问题] CollectionView结合CoreData
时间Thu May 26 13:39:26 2016
我是初学者,想请教一个让我卡很久的问题,官方文件和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://webptt.com/cn.aspx?n=bbs/MacDev/M.1464241168.A.B25.html
※ 编辑: z82101096 (203.187.43.7), 05/26/2016 13:40:48
1F:推 tentenlee: 丢git吧 超难阅读的.. 05/26 19:00
2F:→ Esvent: collectionView的资料更新方式记得是先修改model 05/26 19:49
3F:→ Esvent: 改完之後再呼叫performBatchUpdates 把要新增删除的call 05/26 19:50
4F:→ Esvent: 放在里面 05/26 19:50