当我尝试从Realm数据库中删除项目时,我无法适当更新UICollection View。删除Realm对象后更新UICollectionView
让我们假设一个境界容器List<Child>
类型的children
:
var children = realm.objects(Parent).first!.children
当我想从数据库中删除这个孩子:
try! realm.write {
realm.delete(children[indexPath.row])
}
由collectionView.deleteItemsAtIndexPaths([indexPath])
更新的CollectionView提供了以下错误:
Got error: *** Terminating app due to uncaught exception 'RLMException', reason: 'Object has been deleted or invalidated.'
T他只能通过使用collectionView.reloadData()
来获取更新的collectionView,但这不是我想要的,因为缺少单元格删除的动画。
然而,当我只能在这个容器中,在indexPath.row
通过移除子(不从数据库中删除它):
try! realm.write {
children.removeAtIndex(indexPath.row)
}
与collectionView.deleteItemsAtIndexPaths([indexPath])
作品更新的CollectionView没有问题。
从数据库中删除项目后,更新UICollectionView的最佳方法是什么?
问题是你先从children.removeAtIndex(indexPath.row)中删除它。那么如何在indexPath.row中使用该对象? –
这是真的。但事实上,我不想使用'removeAtIndex',因为我想从数据库中删除它。我提到它是因为这是我能够使collectionView更新工作的唯一方法。 – Gerard
要重新加载集合视图,您必须从子列表中删除对象。这样集合视图就会得到新的列表。 –