21

How can I implement didSelectRowAtIndexPath: or something similar in my Swift app?

I have a populated Table View with several dynamic cells and I want to change views once a certain cell is selected.

I am able to get my head around it in Objective-C, but there is nothing on google to help me with Swift!

pkamb
  • 33,281
  • 23
  • 160
  • 191
Alex
  • 233
  • 1
  • 2
  • 6

3 Answers3

39

You can use didSelectRowAtIndexPath in Swift:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    NSLog("You selected cell number: \(indexPath.row)!")
    self.performSegueWithIdentifier("yourIdentifier", sender: self)
}

Just make sure you implement the UITableViewDelegate.

pkamb
  • 33,281
  • 23
  • 160
  • 191
Christian
  • 22,585
  • 9
  • 80
  • 106
  • Thankyou so much! Google didn't at all, I spent ages looking and just as long trying for myself... This presents me with another issue though. How do I make sure each individual cell performs a different segue? – Alex Feb 10 '15 at 13:20
  • Check the indexPath.row. It gives you the number of the row. Then you can use an if-else. – Christian Feb 10 '15 at 13:21
  • Awesome! Thankyou for the help, I'm finally starting to understand Swift a little better now :) – Alex Feb 10 '15 at 13:24
  • 1
    I would recommend you to read some tableview-tutorials from raywenderlich.com – Christian Feb 10 '15 at 13:25
  • Ill give that a look now! – Alex Feb 10 '15 at 13:27
  • 1
    Would this be the correct code to use? `override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { NSLog("You selected cell number: \(indexPath.row)!") if indexPath.row == 0 { self.performSegueWithIdentifier("MoveToSignIn", sender: self) }` – Alex Feb 10 '15 at 13:49
  • I want to make row 1 perform a different segue, but when i change the code to `indexPath.row == 1` it still performs the first segue? – Alex Feb 10 '15 at 13:53
  • Check if you've changed the segueIdentifier. – Christian Feb 10 '15 at 13:54
  • I have changed it, but using two if statements... is that correct? `override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { NSLog("You selected cell number: \(indexPath.row)!") if indexPath.row == 0 { self.performSegueWithIdentifier("MoveToSignIn", sender: self) } if indexPath.row == 1 { self.performSegueWithIdentifier("MoveTo2", sender: self) }` – Alex Feb 10 '15 at 13:58
  • You should ask another question. That way it will be seen by more Users. – Christian Feb 10 '15 at 13:59
  • Wow, indexPath.row was exactly what I needed! Thanks! – Edward May 31 '16 at 22:02
  • This didn't work for UITableViewController. Anybody know why? – Jack Robson Sep 15 '16 at 13:28
3

This is how I managed to segue from UITableView cells to other view controllers after implementing cellForRow, numberOfRowsInSection & numberOfSectionsInTable.

//to grab a row, update your did select row at index path method to:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    NSLog("You selected cell number: \(indexPath.row)!");

    if indexPath.row == 1 {
        //THE SEGUE 
        self.performSegue(withIdentifier: "goToMainUI", sender: self)
    }
}

Will output: You selected cell number: \(indexPath.row)!

Remember to match the identifier of your segue in story board to the identifier in the function, e.g goToMainUI.

Community
  • 1
  • 1
WHC
  • 31
  • 3
0

Use the following code to select cell at '0' index programmatically for collectionView.

self.collectionView.reloadData()
DispatchQueue.main.async {
  self.collectionView(self.collectionView, didSelectItemAt: IndexPath(item: 0, section: 0))
}
Asad Jamil
  • 198
  • 9