我有一个集合视图,您可以在其中选择要删除的多个单元格。这意味着如果删除了多个单元格,那么在 Realm 中也应该删除多个对象 - 每个单元格 1 个对象。
我有一个函数,它接受一个Int
's 数组,该数组将从集合视图的选定 indexPaths 中填充。
问题是我不确定如何做到这两个
1)删除领域中的对象和
2)在List
没有删除对象的情况下保持最新
我的代码是:
我得到这样的索引路径:
let indexPaths = collectionView.indexPathsForSelectedItems
这是我在 Realm 中获取 indexPaths、更新List
和删除对象的函数。它当前不起作用,因为对象没有被删除。我注意到这removeAll
不会删除任何内容。
func removeVideos(at indexes: [Int]) {
let newVideos = List<Video>()
for (index, video) in favorite!.videos.enumerated() {
if !indexes.contains(index) {
newVideos.append(video)
}
}
let realm = try! Realm()
try! realm.write {
favorite!.videos.removeAll()
newVideos.forEach { newVideo in
favorite!.videos.append(newVideo)
}
}
}
我这样称呼该函数:
removeVideos(at: indexPaths.map { $0.item })
有什么想法吗?