我正在尝试使用 UITableViewDiffableDataSource 实现现有的 coredata 项目。我的 TableView 使用 NSFetchedResultsController 和相应的委托(delegate)方法进行耦合。 我可以使用 diffabledatasource 在 TableView 中列出数据。我的数据源使用如下泛型类型声明
UITableViewDiffableDataSource<String, NSManagedObjectID>
为了在表格 View 中启用编辑模式,我对 UITableViewDiffableDataSource 进行了子类化。我可以从表格 View 中删除单元格,但不能从我的 coreData 中删除单元格。删除单元格的代码如下
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
if let identifierToDelete = itemIdentifier(for: indexPath){
var snapshot = self.snapshot()
snapshot.deleteItems([identifierToDelete])
apply(snapshot)
}
}}
当我删除单元格时,不会调用下面的 NSFetchedResultsControllerDelegate 方法。
func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChangeContentWith snapshot: NSDiffableDataSourceSnapshotReference)
我不确定这是否是将 diffabledatasource 与 NSFetchedResultscontroller 耦合的正确方法。任何帮助将不胜感激。提前致谢
最佳答案
而不是在UITableViewDiffableDataSource
中重写tableView(_:commit:)
实现
override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? { ...
在 View Controller 中,通过索引路径从获取结果 Controller 获取对象。
关于ios - 使用 UIDiffableDataSource TableView 删除项目时,不会调用 NFetchedResultsController 委托(delegate)方法 didChangeContentWith,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59018464/