Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

246
Views
How to explain "completionHandler(true) "

I am following a tutorial online regarding a TableView Cell swipe action. I can explain to myself every line, except completionHandler(true)

This is the code snippet

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

    let deleteAction = UIContextualAction(style: .destructive, title: "Delete") { (action, sourceView, completionHandler) in
        //Delete
        self.restaurantNames.remove(at: indexPath.row)
        self.restaurantLocations.remove(at: indexPath.row)
        self.restaurantTypes.remove(at: indexPath.row)
        self.restaurantIsVisited.remove(at: indexPath.row)
        self.restaurantImages.remove(at: indexPath.row)

        self.tableView.deleteRows(at: [indexPath], with: .fade)
        // Call completion handler to dismiss the action button
        completionHandler(true)
    }

    let shareAction = UIContextualAction(style: .normal, title: "Share") { (action, sourceView, completionHandler) in
        let defaultText = "Just checking in at" + self.restaurantNames[indexPath.row]
        let activityController = UIActivityViewController(activityItems: [defaultText], applicationActivities: nil)
        self.present(activityController, animated: true, completion: nil)
        // Call completion handler to dismiss the action button
        completionHandler(true)
    }

    let swipeConfiguration = UISwipeActionsConfiguration(actions: [deleteAction, shareAction])

    return swipeConfiguration
}

I don't quite understand what does this mean? Thanks in advance

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

All of the existing answers just paste Apple's documentation which answers precisely nothing. It says when it should be called but nothing about what it does to the UI.

As far as I can tell, calling completionHandler with either value will collapse the buttons. You can see this in action by calling the completion handler in a dispatch_after block with a delay. Specifying true vs. false makes no difference in my tests.

I have a feeling that I'm missing something, but Apple's documentation is opaque here.

over 4 years ago · Santiago Trujillo Report

0

Explanation: You call the completion handler with true to indicate you performed the action or false if you could not for some reason.

Check below demo code work:

func contextualToggleFlagAction(forRowAtIndexPath indexPath: IndexPath) -> UIContextualAction {

    var email = data[indexPath.row]

    let action = UIContextualAction(style: .normal,
                                    title: "Flag") { (contextAction: UIContextualAction, sourceView: UIView, completionHandler: (Bool) -> Void) in

        if email.toggleFlaggedFlag() {

            self.data[indexPath.row] = email
            self.tableView.reloadRows(at: [indexPath], with: .none)

            completionHandler(true) // your task is successfully done.
        } else {

            completionHandler(false) // With some reason you unable to perform task so now you returning false.
        }
    }

    action.image = UIImage(named: "flag")
    action.backgroundColor = email.isFlagged ? UIColor.gray : UIColor.orange
    return action
}
over 4 years ago · Santiago Trujillo Report

0

Third parameter of the UIContextualAction handler is a closure that tells UIContextualAction whether or not you performed the action. By calling completionHanlder(true) you're notifying UIContextualAction that you performed the requested action. If, for example, there was an error in your handler that precluded you from performing the desired action, you can notify the handler by calling completionHandler(false).

Documentation: https://developer.apple.com/documentation/uikit/uicontextualaction/handler

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!