Home > OS >  Disable delete option for specific UITableViewCell in a UITableView
Disable delete option for specific UITableViewCell in a UITableView

Time:09-17

for UITableViews I have added three difference cells

How to Enable delete option for specific cell

 func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {

    switch dataSource[indexpath.section].menu {
        case  "Attachment":
            if editingStyle == AttachmentTableViewCell.EditingStyle.delete {
                attachmentList.remove(at: indexPath.row)
                tableView.deleteRows(at: [indexPath], with: UITableView.RowAnimation.automatic)
            }
            break
        default:
            break

    }
}

In this case it's shows Delete option for other cell too. How to stop showing the Delete option on swipe to other UITableViewCell.

CodePudding user response:

Implement tableView(_:editingStyleForRowAt:) and return none for the index paths which should not show the delete option.

CodePudding user response:

This worked for me.

Overriding this func from UITableViewController or implementing it UITableViewDelegate

override func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration?

The indexpaths were you want an action return a UISwipeActionsConfiguration else return nil.

like this

override func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
         
    // return nil on specific rows
    guard indexPath.row != 1 else {
        return nil
    }
        
    let contexualAction = UIContextualAction(style: .normal, title: "Action") { _, _, _ in
        // Do Action
    }
    let swipeAction = UISwipeActionsConfiguration(actions: [contexualAction])

    return swipeAction
}
  • Related