1
votes

I have a view controller with a table view embedded inside it, although it is always the same ten cells, I made it dynamic as you cannot make static table view cell inside a UIViewController. My question is, in prepare(for:) how can I segue to a view controller based on which cell was tapped? Each cell should navigatie to a completely different VC, but I think having ten segues is a mess.

Is there a better way?

1
yes this become more mess but if you want to use segue then you can differentiate them with segue . identifier - Ram Mani
I prefer not to use this many segues. can I do something in didSelectRow(at:), maybe based on indexPath user tapped, say which vc? - isaacthedev
you cannot make static table view cell inside a UIViewController. Why not? - vadian
I think you can using a container view, but I don't think it would help my issue, not sure. - isaacthedev
Actually it was a rhetorical question. Of course you can embed an UITableView with static cells in a UIViewController. - vadian

1 Answers

2
votes

You can implement the UITableViewDelegate protocol to actually detect the cell being tapped:

extension MyOriginViewController {
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        // I'm assuming you have only 1 section
        switch indexPath.row {
            case 0:
                // Instantiate the VC
                let vc = storyboard?.instantiateViewController(withIdentifier: "MyViewControllerIdentifier")
                // Present the view controller, I'm using a UINavigationController for that
                navigationController?.push(vc, animated: true)
            case 1:
                // Another VC and push or present modally
                let vc = ...
                self.present(vc, animated: true, completion: nil)
            default:
                // Default case (unconsidered index)
                print("Do something else")
        }
    }
}

So if you have this: Storyboard

You can do:

let vc = storyboard?.instantiateViewController(withIdentifier: "MyNavController")
self.navigationController?.present(vc, animated: true, completion: nil)