Table View row to take me to a different table view.

I am creating an app that has a main view controller. I created a table view with 3 sections. Each section has 1 row. When I click on one row in one of the sections I want it to take me to another view controller. When I click on a different sections row I want it to take me to another view controller. And when I click on the last sections row I want it to take me to a different view controller. How do I do this with Swift 4.2?

Accepted Reply

One way to do this.


Create 3 segues from the View Controller holding the tableview (control-drag from the yellow icon at upper top left of the view controller, to each destination VC).


Give an ID to the segues (Segue0, Segue1, Segue2).


In tableView (didSelectRow), implement as follows:


    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    
        if indexPath.section == 0 {
          performSegue(withIdentifier: "Segue0", sender: self)
        } else if indexPath.section == 1 {
               performSegue(withIdentifier: "Segue1", sender: self)
        } else if indexPath.section == 2 {
               performSegue(withIdentifier: "Segue2", sender: self)
         } else { return }
  }


Of course, set the destination properties as needed in prepare for segue.

Replies

One way to do this.


Create 3 segues from the View Controller holding the tableview (control-drag from the yellow icon at upper top left of the view controller, to each destination VC).


Give an ID to the segues (Segue0, Segue1, Segue2).


In tableView (didSelectRow), implement as follows:


    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    
        if indexPath.section == 0 {
          performSegue(withIdentifier: "Segue0", sender: self)
        } else if indexPath.section == 1 {
               performSegue(withIdentifier: "Segue1", sender: self)
        } else if indexPath.section == 2 {
               performSegue(withIdentifier: "Segue2", sender: self)
         } else { return }
  }


Of course, set the destination properties as needed in prepare for segue.