In UIKit, how do you present a confirmation dialog when the user is swiping to delete a table view cell?

Is there a UIKit equivalent to SwiftUI's confirmationDialog(_:isPresented:titleVisibility:actions:)?

I did it using semaphore. There are now better ways in iOS15 with async/wait.

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
   
   if editingStyle == .delete {
       var delete = false

       let titre = "Suppress row"
       let detailMsg = "Do you confirm row suppression?"
       let controller = UIAlertController(title: titre, message: detailMsg, preferredStyle: .alert)

       DispatchQueue.global(qos: .userInitiated).async {
          let semaphore = DispatchSemaphore(value: 0) // So that delete test be completed
           let okAction = UIAlertAction(title: "I confirm", style: .destructive) { (handler) in
               delete = true
               semaphore.signal()
           }
          let cancelAction = UIAlertAction(title: "No!", style: .default) { (handler) in
               delete = false
               semaphore.signal()
           }
           DispatchQueue.main.async {
               controller.addAction(okAction)
               controller.addAction(cancelAction)
               self.present(controller, animated: true, completion: nil)
          }
           
           let _ = semaphore.wait(timeout: .distantFuture)
           if delete {
            // Proceed with deletion
           }
       }
   }
}

Please note that from an HIG perspective, the confirmation is already build in to swipe actions. If an action can be undone, the full swipe can be used, however if an action is destructive, does not provide undo and / or needs confirmation, disabling this gives you what you want: the user can swipe open and then tab the delete action to confirm. It's already a two step interaction for this reason.

Note that dispatch-asyncing to a global queue to present your alert means that your app’s code will call into UIKit from a non-main thread. This is unsupported behavior and will likely result in subtle errors.

Instead, present the alert in your callback function above, and place the deletion code inside the handler of the UIAlertAction. That way all your code will run on the main thread.

In UIKit, how do you present a confirmation dialog when the user is swiping to delete a table view cell?
 
 
Q