如何从tableview单元格传递数据

时间:2017-11-07 03:08:39

标签: swift tableview segue cell

我想将tableview单元格的indexPath.row从一个viewController转换为另一个viewController。

var index = 0

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    index = indexPath.row
    performSegue(withIdentifier: "cellIndex", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.destination is trackSavingViewController{
        let TSController = segue.destination as! trackSavingViewController
        TSController.indexCell = index
    }
}

但所有剂量只是传递原始值(index = 0),而不是所选行的值。我该怎么做来修复代码?

2 个答案:

答案 0 :(得分:0)

准备(for segue)

let cell = sender as! UITableViewCell
let indexPath = tableView.indexPath(for: cell)
TSController.indexCell = indexPath.row

答案 1 :(得分:0)

func tableView(_ tableView:UITableView,didSelectRowAt indexPath:IndexPath){         让storyboard = UIStoryboard(名称:" Main",bundle:nil)         让Dvc = storyboard.instantiateViewController(withIdentifier:" DataVC")为! DataVC

    Dvc.getRollNo = arrayStudent[indexPath.row].strRollNo
    Dvc.getName = arrayStudent[indexPath.row].strName

    self.navigationController?.pushViewController(Dvc, animated: true)
}