1

I want to present a different view when selecting each Cell in a Table View.

enter image description here

I figured out that I have to use this function:

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



}

What should I add inside the function to make this work?

kb920
  • 3,039
  • 2
  • 33
  • 44
Mariah
  • 573
  • 3
  • 10
  • 26
  • http://stackoverflow.com/questions/22759167/how-to-make-a-push-segue-when-a-uitableviewcell-is-selected look that – Prashant Tukadiya Apr 13 '16 at 08:44
  • You can detect your cell by your `indexPath`, in your `didSelectRowAtIndexPath` (not `didDeselect...`), and follow the guide above – Tj3n Apr 13 '16 at 08:48

2 Answers2

1

First of all, you need didSelectRowAtIndexPath and inside the function you could use indexPath.row to know witch cell was tapped and then you need to create and push the view you want

let viewControllerObj = self.storyboard?.instantiateViewControllerWithIdentifier("ViewControllerIdentifier") as? ViewController
self.navigationController?.pushViewController(viewControllerObj!, animated: true)

also, make sure your navigationController is not nil

...hope it helps

Hasya
  • 9,792
  • 4
  • 31
  • 46
razvan
  • 355
  • 4
  • 19
  • indexPath.row represents the index of the cell in the table. Usually the cell should be represented by a ViewModel. Otherwise saying, you need to have an array of viewModels to use, when building cell contents. – Oleg Danu Apr 13 '16 at 09:22
0
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) 
{

    if indexPath.row == 0
    {
        let objOne = self.storyboard?.instantiateViewControllerWithIdentifier("ID_OneScene") as? OneScene

        self.navigationController?.pushViewController(objOne!, animated: true)

    }
    else if indexPath.row == 1
    {
        let objTwo = self.storyboard?.instantiateViewControllerWithIdentifier("ID_TwoScene") as? TwoScene

        self.navigationController?.pushViewController(objTwo!, animated: true)

    }
    else
    {
        let objOther = self.storyboard?.instantiateViewControllerWithIdentifier("ID_OtherScene") as? OtherScene

        self.navigationController?.pushViewController(objOther!, animated: true)

    }

}
Hasya
  • 9,792
  • 4
  • 31
  • 46