rohaldb
rohaldb

Reputation: 588

Sending indexPath.row from didselectRowAtIndexPath to prepareForSegue via sender

I have seen many solutions however i cant get this right. I want to call prepareForSegue from the didSelectRowAtIndexPath function when a cell is tapped. Then i want to retrieve the row index that was tapped on, from inside the prepareForSegue function so i can use it to pass information to another view. I think i am calling the function correctly but there is something going wrong in my prepareForSegue function. I know its an easy solution i am just very stuck. Heres my current code

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    performSegueWithIdentifier("notificationsToAnswers", sender: self)
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    //the below line is incorrect and should be something else
    let selectedIndex = tableView.indexPathForSelectedRow?.row
    //pass data on using the index
}

Upvotes: 1

Views: 1102

Answers (1)

vien vu
vien vu

Reputation: 4347

You should pass like this:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    performSegueWithIdentifier("notificationsToAnswers", sender: indexPath)
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "notificationsToAnswers" {
        //the below line is incorrect and should be something else
       let selectedIndexPath = sender as! NSIndexPath
       let index = selectedIndexPath.row
       //pass data on using the index
    }

}

Upvotes: 2

Related Questions