Answer the question
In order to leave comments, you need to log in
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
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)
}
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 questionAsk a Question
731 491 924 answers to any question