首页 > 解决方案 > 有没有办法在 UITableview Diffable 数据源中滑动删除/添加操作到单元格中?

问题描述

我正在尝试将滑动操作添加到我的表格视图中。当我使用简单的 tableview 数据源方法时,它工作得很好(trailingSwipeActionsConfigurationForRowAt)。但是当我用 Diffable 数据源尝试同样的事情时,它甚至没有调用刷卡的方法。我使用断点进行跟进,但没有奏效。我正在使用 swift 5 (UIKit)、Xcode 12.4

标签: iosswiftuikitswift5

解决方案


我通过以下方法解决了这个问题:使用了 diffabled 数据源的子类,如下所示。

class GrocDataSource: UITableViewDiffableDataSource <GrocListSections, Grocs> {
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
    if indexPath.section == 0 || indexPath.section == 2 {
        return true
    } else {
        return false
    }
}

}

将此类实现到数据源中:

datasource = GrocDataSource(tableView: grocTableView, cellProvider: { (tableView, indexPath, grocs) in
guard let cell = tableView.dequeueReusableCell(withIdentifier: "GrocNameCell", for: indexPath) as? GrocNameCell else {return UITableViewCell()}
return cell })

推荐阅读