Search code examples
swiftuitableviewsegue

Swift segue negating other functions connected to segue object


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?


Solution

  • 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