如何在使用 UITableViewDiffableDataSource 时移动 UITableView 单元格?

Sou*_*sam 1 uitableview uikit ios swift

我正在尝试使我的可移动,但它需要协议中tableView cells的 2 或 3 个功能,如果我尝试在我的中实现委托,它将要求新的.UITableViewDataSourceviewControllernumberOfRowsInSectioncellForRowAtIndexPathUITableViewDiffableDataSource

在使用 new 时如何实现这种行为UITableViewDiffableDataSource

Tun*_*Fam 5

我可以通过UITableViewDiffableDataSource像这样子类化该类来做到这一点:

class MyDataSource: UITableViewDiffableDataSource<Int, Int> {

    override func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
        // your code to update source of truth
        // make a new snapshot from your source of truth
        // apply(snapshot, animatingDifferences: false)
    }
}

Run Code Online (Sandbox Code Playgroud)

然后您可以使用override实施所需的任何数据源方法。