从didselectRowAtIndexPath发送indexPath.row以通过sender发送prepareForSegue

时间:2016-02-03 07:49:05

标签: ios swift uitableview segue sender

我见过许多解决方案,但我无法做到这一点。我想在点击一个单元格时从didSelectRowAtIndexPath函数调用prepareForSegue。然后我想从prepareForSegue函数内部检索被轻击的行索引,以便我可以使用它将信息传递给另一个视图。我想我正在调用函数,但是我的prepareForSegue函数出现了问题。我知道这是一个简单的解决方案,我只是非常困难。继承我目前的代码

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
}

1 个答案:

答案 0 :(得分:2)

你应该这样传递:

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
    }

}