Segue not getting selected row number

后端 未结 3 1699
花落未央
花落未央 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:59

    //In didSelectRowAtIndexPath, you use this code:
    
    func tableView(tvMain: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    
            self.performSegueWithIdentifier("yourSegueName", sender: nil)
    
        }
    
    //And prepareForSegue to get IndexPath and send your value
    
    override func prepareForSegue(segue: UIStoryboardSegue,
            sender: AnyObject!){
    
                let indexPath : NSIndexPath = self.yourTableView.indexPathForSelectedRow()!
                //make sure that the segue is going to secondViewController
                let detailsVC = segue.destinationViewController as DetailTableViewController
                detailsVC.selectedPersonName = "\(people[indexPath.row])"
    
        }
    

提交回复
热议问题