當我嘗試從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
要重新加載集合視圖,您必須從子列表中刪除對象。這樣集合視圖就會得到新的列表。 –