M
M
Maxim2019-01-02 10:29:22
iOS
Maxim, 2019-01-02 10:29:22

How to switch VC on click. on a tableview cell?

Hello!
I have table cells that are added with an initializer. That is, there are no cells in the storyboard. How do I switch VC? That is, by clicking on cell 1, firstViewController appears, if on cell 2, then secondViewController?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
@
@antoo, 2019-01-02
_

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  let tableRow = indexPath.row

  var controllerToPresent: UIViewController
  if tableRow == 0 {
    // инстанциируем через Storyboard или как угодно
    controllerToPresent = FirstViewController()
  } else {
    controllerToPresent = SecondViewController()
  }

  self.present(controllerToPresent, animated: true)
}

D
Dima Grib, 2019-01-02
@YeahGarage

Depending on how you make transitions, segways or presentations, here is an example of prizes

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        guard let indexPath = tableView.indexPathForSelectedRow  else { return }

        DispatchQueue.main.async {
            self.navigationController?.pushViewController(YOUR CONTROLLER, animated: true)
        }
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question