Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

247
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda