Search code examples
swiftswift4

TableView cell, swipeactions and different segues Swift 4


In ViewController A I have a TableView with two trailingSwipeActions and one leadingSwipeAction. If you tap on the cell there is a segue to ViewController B. The trailingSwipeActions call ActionSheets, handled inside ViewController A. The segue is executed from within the 'prepare for segue' function. That all works super.

Now I want to add another segue for if you perform the leadingSwipeAction. That segue has to lead to ViewController C. I can't add another segue in the Storyboard, from the customcell to ViewController C... then when I click on the cell... just one segue gets called (and not the other). So... what to do.

Here is the relevant part of my code:

override func tableView(_ tableView: UITableView,
                        leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration?
{
    let weightAction = UIContextualAction(style: .normal, title:  "Gewicht", handler: { (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
        success(true)
    })
    weightAction.image = UIImage(named: "Scale")
    weightAction.backgroundColor = UIColor(red: 0/255, green: 122/255, blue: 255/255, alpha: 1)
    return UISwipeActionsConfiguration(actions: [weightAction])
}

What I want: if you tap on the icon named 'Scale' you trigger a segue to ViewController C.

Also my code for the 'prepare for segue' function (for the segue to ViewController B):

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "detailSegue"{
        let rowClicked = (self.tableView.indexPathForSelectedRow?.row)!
        let destVC = segue.destination as! UINavigationController
        let slideVC = destVC.topViewController as! detailViewController
        slideVC.araKuiken = araKuikens[rowClicked]
    }
}

So how can I add a segue to ViewController C, that gets called when I tap the 'scale' button?


Solution

  • If you want your weightAction to trigger a segue to viewController C you will just need to perform that segue inside the handler block like so:

    let weightAction = UIContextualAction(style: .normal, title:  "Gewicht", handler: { (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
        self.performSegue(withIdentifier: "viewControllerC", sender: ac)
        success(true)
    })
    

    Then, in your prepare code you'd want to handle this possibility as well:

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
      if segue.identifier == "detailSegue"{
        let rowClicked = (self.tableView.indexPathForSelectedRow?.row)!
        let destVC = segue.destination as! UINavigationController
        let slideVC = destVC.topViewController as! detailViewController
        slideVC.araKuiken = araKuikens[rowClicked]
      } else if segue.identifier == "viewControllerC" {
        //Perform your preparation code here
      }
    }
    

    Of course you'll still need to create the segue in the Storyboard, as you mentioned, by dragging from the yellow circle at the top of viewControllerA to viewControllerC, and giving it the proper name (I used "viewControllerC" as an example).