iosswiftuitableviewuitableviewrowaction

How to add swipe actions for table view cell in swift 5


I want to add Edit action to display when the user swipes a table row. I used to be able to use the tableView(_:editActionsForRowAt:) method, but it is now deprecated. And in the tableView(_:commit:forRowAt:) method, there is no action I need. How can I add actions for my cells?


Solution

  • Try the following method:

    func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
            let item = UIContextualAction(style: .destructive, title: "Delete") {  (contextualAction, view, boolValue) in
                //Write your code in here
            }
            item.image = UIImage(named: "deleteIcon")
    
            let swipeActions = UISwipeActionsConfiguration(actions: [item])
        
            return swipeActions
        }