問題
twtterクライアントを作っていて、timeline中の1ツイートをタッチすると、そのツイートのみを表示する画面に遷移する機能を作ろうしていました。
しかし、xibでtableViewのcellを作っていたので、
- cellから他の画面へのsegueを繋げないこと
- cell自体を@IBActionでつないでActionを設定すること
ができないことに気がつきました。(prototypecellを使うと、同storyboard内でsegueを繋げるんですけどね。cellを使いまわしたかったのです。)
解決法
実は、tableViewControllerは、cellが選ばれたときの処理を各メソッドを用意しています。
それが、
tableViewController.swift
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
}
です。
このメソッド内にかけばOK。
例えば、このページでManual Segueを"toDetailTweet"と言うidentifierで作っておいてから、、
以下のようにperformSegue()メソッドでsenderをcellに指定すればOK
tableViewController.swift
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: "toDetailTweet", sender: tableView.cellForRow(at: indexPath))
}