Search code examples
iosswiftuikitswift5

Is there any way to swipe to delete/ Add actions into cell in UITableview Diffable datasource?


I am trying to add swipe actions into my tableview. when I used simple tableview datasource method it worked fine (trailingSwipeActionsConfigurationForRowAt). but when I tried the same thing with Diffable datasource it didn't even call the method for swiping. I used breakpoints to get followup but it didn't work. I am using swift 5 (UIKit), Xcode 12.4


Solution

  • I solved this issue by following method: Used subclass of diffabled datasource as follows.

    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
        }
    }
    

    }

    Implemented this class into datasource as:

    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 })