TableView Cell not move on button click

I have a tableView with cells. I’ve added some buttons in the editActionsForRowAtIndexpath so that when I click one, an actionsheet shows up. The problem is I don’t know how to stop the cell from closing when I click the button. When I swipe, it opens so I can view the buttons. I click a button, the actionSheet shows up and in the background the cell closes.

Hi @dadixon, I’m trying to visualize what the issue is and can kinda understand your problem. Could you clarify what you mean by the cell closing when clicked?

Best,
Gina

The cell closing meaning if I swipe left and click the button, the cell will automatically slide right once the action sheet shows

I’m trying to reproduce the mail app. When you swipe left on an email it gives you options to press. If you press the more option, an action sheet shows up and the table cell doesn’t move. I’m trying to do that where it doesn’t move on click

Hi @dadixon, could you share code with us please? I do not have any examples on how to achieve what you are explaining unfortunately.

Best,
Gina

I was curious about this, did some Googling, and found this statement:

The new style swipe actions added in iOS 11 and used by Apple in Mail and other apps can be on either side and can include images.

Swipe actions are the new way to do it, instead of Edit Actions. The handler for swipe actions is passed a completion handler, which you call with either true or false - true indicating the action was completed. That means it doesn’t close the row until the handler is done. I am pretty sure that is how Mail is doing it.

override func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) → UISwipeActionsConfiguration? {
let deleteAction = UIContextualAction.init(style: UIContextualAction.Style.normal, title: “Delete”, handler: { (action, view, completion) in

        completion(false)
        
        let alert = UIAlertController(title: "Delete Employee?", message: "Delete Employee", preferredStyle: .alert)
        
        alert.addAction(UIAlertAction(title: "Yes", style: .default, handler: nil))
        alert.addAction(UIAlertAction(title: "No", style: .cancel, handler: nil))
        
        self.present(alert, animated: true)
        
        
    })
    
    deleteAction.backgroundColor = UIColor.red
    
    let config = UISwipeActionsConfiguration(actions: [deleteAction])
    
    config.performsFirstActionWithFullSwipe = false
    return config
}
      // completion(false)
      let alert = UIAlertController(title: "Delete Employee?", message: "Delete Employee",
                                    preferredStyle: .alert)
      alert.addAction(UIAlertAction(title: "Yes", style: .default,
                                    handler: {(_) in completion(true)} ))
      alert.addAction(UIAlertAction(title: "No", style: .cancel,
                                    handler: nil ))

No completion() = row not closed. :sunglasses:

Thanks. That works as expected now.

This topic was automatically closed after 166 days. New replies are no longer allowed.