Segue not getting selected row number

后端 未结 3 1700
花落未央
花落未央 2021-02-09 00:37

I am passing data from a table view controller to a detail view. I tried using indexPath.row directly in my prepareForSegue method, however it displays

3条回答
  •  无人共我
    2021-02-09 00:55

    The best way to do this kind of thing is not to use the delegate.

    Updated Swift 4+

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let selectedIndex = tableView.indexPath(for: sender as! UITableViewCell)
        // Do your stuff with selectedIndex.row as the index
    }
    

    Original Answer

    override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
        let selectedIndex = self.tableView.indexPathForCell(sender as UITableViewCell)
        // Do your stuff with selectedIndex.row as the index
    }
    

提交回复
热议问题