0

In a table view controller, I'm trying to get the index of the row that is selected before I segue (will pass this info in prepare for segue eventually). I have implemented this function:

var selectedQuestionCell: IndexPath?

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    selectedQuestionCell = indexPath
}

This was working great for me initially. I was able to snag the index of the selected row in my table. However, I want to perform a segue when a row is tapped which will transfer to another View. I connected a segue to my cell (dynamic prototypes). After connecting a segue, my override function no longer executes! I'm really new to Table View Controllers, so I'm not sure if there's anyway I can manually execute this.

To summarize: my problem is after connecting a segue to my cells, the override function to get their index is no longer called. How can I fix this?

Marcy
  • 4,611
  • 2
  • 34
  • 52
Birdman
  • 1,404
  • 5
  • 22
  • 49
  • 1
    You can use `pushViewController` without using segue, see https://stackoverflow.com/a/24038621/5523205 – Shahrukh Feb 11 '19 at 05:08
  • 1
    check if this helps, https://stackoverflow.com/questions/46617760/swift-4-preparefor-segue-being-called-after-viewdidload – Aryan Sharma Feb 11 '19 at 05:19

1 Answers1

1

Try these thing

Create a new segue from your tableview to another view and name it as you like.

Now add this code inside your didSelectRowAt

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let viewController = storyboard?.instantiateViewController(withIdentifier: "your_segue_name_here") as! YourViewControllerName
    self.navigationController?.pushViewController(viewController, animated: true)

    }

Please make sure that your tableview is connected with navigation controller.

To do that select your tableView then click on Editor -> Embed In -> Navigation Controller from your Xcode.

I hope it works for you.

: D

Adi.S
  • 280
  • 1
  • 13