1

I am sorry for echoing same question but after a lot of time spending I could not figure out the issue.

Suppose, a UITableView have 2 table_cell s and I want to navigate different UIViewController from each table_cell. How do I do that?

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

                if indexPath.row == 0 {

                    // go to DetailVC_1

                } else if indexPath.row == 1 {

                    // go to DetailVC_2

        }

enter image description here

Asim Roy
  • 9,915
  • 4
  • 28
  • 40
  • try this:http://stackoverflow.com/questions/32014001/how-to-push-a-new-view-controller-when-a-table-cell-is-tapped – johny kumar Dec 09 '15 at 12:30

2 Answers2

3

Either present a new view controller manually:

self.presentViewController(controller, animated:true, completion:nil)
// or push on a navigation controller
self.navigationController.pushViewController(controller, animated:true)

Or perform a segue you set up in a storyboard earlier. (Drag from the view controller to another view controller in your storyboard).

self.performSegueWithIdentifier("MyIdentifier", sender: self)

When using storyboards you can also use multiple cell types and setup a segue from each cell type to a new controller. This way the new controller will be presented automatically. The only code you might need to use in that case would be a prepareForSegue method where you pass the correct information to the presented controller before the segue is performed.

Joris Kluivers
  • 11,894
  • 2
  • 48
  • 47
0

Try this:

if indexPath.row == 0 {
         self.performSegueWithIdentifier("firstViewController", sender: self)
} else if indexPath.row == 1 {
         self.performSegueWithIdentifier("secondViewController", sender: self)
}

If you have several view controllers, you can call them viewController0 and viewController1 etc. Then you can have a string "viewController" + String(indexPath.row). Hope this helps.

Pranav Wadhwa
  • 7,666
  • 6
  • 39
  • 61