Uitableview tableView(canEditRowAt)不再使用tableViewDiffableDataSource

Uitableview tableView(canEditRowAt)不再使用tableViewDiffableDataSource,uitableview,delete-row,editing,uitableviewdiffabledatasource,Uitableview,Delete Row,Editing,Uitableviewdiffabledatasource,我创建了一个相当简单的tableView,用于为项目模型选择类别。昨天一切正常。今天,我一直在尝试将tableView数据源切换到UITableViewDiffableDataSource,因为我想了解API。我已经备份并运行了整个tableView,但我无法再编辑我的行 当我点击导航栏中的编辑按钮时,setEditing逻辑作为newCategoryButton被禁用,当我再次点击它时被启用。但是,我永远无法滑动删除,并且在编辑模式下,删除图标不会显示在我的行旁边 我尝试删除setEditin

我创建了一个相当简单的tableView,用于为项目模型选择类别。昨天一切正常。今天,我一直在尝试将tableView数据源切换到
UITableViewDiffableDataSource
,因为我想了解API。我已经备份并运行了整个tableView,但我无法再编辑我的行

当我点击导航栏中的编辑按钮时,
setEditing
逻辑作为
newCategoryButton
被禁用,当我再次点击它时被启用。但是,我永远无法滑动删除,并且在编辑模式下,删除图标不会显示在我的行旁边

我尝试删除
setEditing
,清空
commit editingStyle
,只需将
canEditRow
设置为
return true
,但仍然没有结果

任何帮助都将不胜感激。谢谢!

override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
    let section = dataSource.snapshot().sectionIdentifiers[indexPath.section]
    if section == .noneSelected {
        return false
    } else {
    return true
    }
}

override func setEditing(_ editing: Bool, animated: Bool) {
    super.setEditing(editing, animated: true)
    if editing == true {
        newCategoryButton.isEnabled = false
    } else {
        newCategoryButton.isEnabled = true
    }
}

override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == .delete {
        categories.remove(at: indexPath.row)
        tableView.deleteRows(at: [indexPath], with: .automatic)
    }
}

override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
    return true
}

override func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
    let movedCategory = categories.remove(at: sourceIndexPath.row)
    categories.insert(movedCategory, at: destinationIndexPath.row)
}

问题在于
canEditRowAt
是一种数据源方法。您(视图控制器)现在不是数据源;可扩散数据源是。您需要在可扩散数据源中实现此方法。这通常是通过对diffable数据源类进行子类化来完成的,这样您就可以重写此方法。否则,可扩散数据源只返回其默认值,即
false
,这就是您当前无法编辑任何行的原因。

下面是一个示例:嘿,谢谢。这样就解决了无法编辑tableView的问题。然而,它创建了一个新的属性,不再在VC中更新我的属性,而且我似乎无法从datasource子类中的override函数中对它们进行变异,这是可能的,因为它们尚未分配。这需要一个全新的问题。:)