Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

231
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda