2017-11-07 18 views
0

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 
    } 
} 

しかし、すべては、選択された行の値ではなく、元の値(インデックス= 0)を単に通過しているだけです。コードを修正するにはどうすればよいですか?

答えて

0

(セグエのために)準備この中に試してみてください

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

それは言う、「未解決識別子 『テーブルビュー』の使用」 –

+0

リンクあなたのテーブルビューをあなたのViewControllerクラスにまたはあなたはそれをそれが動作 – Sherman

+0

を命名するとき、あなたのテーブルビューには、別の名前を持っています!どうもありがとうございます;) –

0

にfuncのtableView(_のtableView:のUITableView、didSelectRowAt indexPath:IndexPath){ LET絵コンテ= UIStoryboard(名称: "メイン"、バンドル:ゼロ) Dvc = storyboard.instantiateViewController(withIdentifier: "DataVC")を次のようにします。 DataVC

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

    self.navigationController?.pushViewController(Dvc, animated: true) 
} 
関連する問題