How do I use a table view to go to different view controllers?

Solution 1:

Yes, didSelectRowAt function is the best way to identify which cell or row is selected at the time.

  • didSelectRowAt Function should be like below

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.row == 0 {
           performSegue(withIdentifier: "FirstViewController", sender: nil)
        } else if indexPath.row == 1 {
           performSegue(withIdentifier: "SecondViewController", sender: nil)
        }
    }
    
  • Segue Prepare Function Should be Like Below

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
       if segue.identifier == "FirstViewController" {
          let vc = segue.destination as? FirstViewController
       } else if segue.identifier == "SecondViewController" {
          let vc = segue.destination as? SecondViewController
       } 
    

Hope, It Helps :)