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

标签 ios swift uitableview uikit

我正在尝试让我的 tableView cells 可移动,但它需要来自 UITableViewDataSource 协议(protocol)的 2 或 3 个函数,如果我试图在我的 中实现委托(delegate)viewController 它将请求 numberOfRowsInSectioncellForRowAtIndexPath 函数,新的 UITableViewDiffableDataSource 已经涵盖了这些函数。

如何在使用新的 UITableViewDiffableDataSource 时实现此行为?

最佳答案

我能够通过子类化 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)
    }
}

然后您可以覆盖实现所需的任何数据源方法。

关于ios - 如何在使用 UITableViewDiffableDataSource 时移动 UITableView 单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57510622/

相关文章:

objective-c - 在 View Controller 之间传递应用程序上下文

ios - 下拉显示 View

ios - 从另一个导航 Controller 将 View Controller 弹出到另一个 View Controller

swift - 子类化另一个 View Controller ?

ios - NSCoding 协议(protocol)一致性的类扩展

ios - 现代时代 UITableView 动画

ios - 如何从 UITableViewCell 转换到另一个 View Controller

ios - 带有 swift 的 iOS 基本 BLE 通信应用程序

objective-c - 在 iOS 中设置标签文本时遇到问题

jquery - CSS Transition 相当于 jQuery fadeIn(), fadeOut(), fadeTo()