Dreamjar
Dreamjar

Reputation: 307

Table View To Custom View Controller?

I Wanted to know if I have two cells in a tableview, and I want that if i click the first cell it seagues to specific view controller, and if I click the second cell it seagues to another view controller. How can I do that?

Upvotes: 1

Views: 46

Answers (2)

Dharmesh Kheni
Dharmesh Kheni

Reputation: 71854

You can do it this way in didSelectRowAtIndexPath method:

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

    switch indexPath.row {
    case 0 :
        println("First cell")
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let vc = storyboard.instantiateViewControllerWithIdentifier("FirstViewController") as! UIViewController
        self.presentViewController(vc, animated: true, completion: nil)
    case 1 :
        println("Second Cell")
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let vc = storyboard.instantiateViewControllerWithIdentifier("SecondViewController") as! UIViewController
        self.presentViewController(vc, animated: true, completion: nil)
    default:
        println("No cell selectd")
    }
}

HERE is your sample project.

Upvotes: 2

Ondřej Mirtes
Ondřej Mirtes

Reputation: 5626

Call performSegue:withIdentifier: from the method didSelectRowAtIndexPath in your view controller. You can conditionally call segues with different identifiers in your code.

Upvotes: 0

Related Questions