如果单元格被触摸 – 移动另一个VC
如果细胞被触摸 – 在另一个VC上移动,我该怎么做? 我试图做,但我没有工作。 这是我的代码
override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.identifier == "transData" { let destination = segue.destination as! WinnerViewController let cell = sender as? UITableViewCell if cell == cell { let indexPath = tableView.indexPath(for: cell!) if indexPath == indexPath { let productName = users[(indexPath?.row)!] destination.winner = productName.name } } }
当我触摸每个细胞 – 它不动。 请帮忙
当单元格被轻敲时,有两个选项可以执行segue。
-
segue从表视图单元连接到目标控制器
在这种情况下,单元格作为
sender
parameter passingoverride func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.identifier == "transData", let cell = sender as? UITableViewCell { let destination = segue.destination as! WinnerViewController let indexPath = tableView.indexPath(for: cell)! let productName = users[indexPath.row] destination.winner = productName.name } }
-
segue从源控制器连接到目标控制器
在这种情况下,您必须实现
didSelectRowAt
,调用performSegue(withIdentifier
并将索引path作为sender
参数func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { performSegue(withIdentifier: "transData", sender: indexPath) }
然后
prepare(for
必须override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.identifier == "transData", let indexPath = sender as? IndexPath { let destination = segue.destination as! WinnerViewController let productName = users[indexPath.row] destination.winner = productName.name } }
Hi you can do like this : take Indexpath as golbal variable eg : var indexPathTemp : IndexPath? func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { indexPathTemp = indexPath performSegueWithIdentifier("transData", sender: self) } override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.identifier == "transData" { let destination = segue.destination as! WinnerViewController let productName = users[(indexPath?.row)!] destination.winner = productName.name } }
编辑
@vadian的答案是详细而正确的。 以下是从视图控制器导航到另一个的其他方法。
如果使用segue
,并且想要导航到WinnerViewController :
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { tableView.deselectRow(at: indexPath, animated: true) performSegue(withIdentifier: "YOUR_SEGUE_IDENTIFIER", sender: indexPath) } override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.identifier == "YOUR_SEGUE_IDENTIFIER", let indexPath = sender as? IndexPath { let destinationVC = segue.destination as? WinnerViewController let productName = users[indexPath.row] destinationVC.winner = productName.name } }
如果您使用Storyboard
和pushViewController
:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { let productName = users[indexPath.row] let storyboard = UIStoryboard(name: "Main", bundle: nil) guard let destinationVC = storyboard.instantiateViewController(withIdentifier: "YOUR_WINNER_VIEW_IDENTIFIER") as? WinnerViewController else { return} destinationVC.winner = productName.name navigationController?.pushViewController(destinationVC, animated: true) }
如果您使用Interface Builder
和pushViewController
:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { let productName = users[indexPath.row] let destinationVC = WinnerViewController(nibName: "YOUR_WINNER_VIEW_NAME", bundle: nil) destinationVC.winner = productName.name navigationController?.pushViewController(destinationVC, animated: true) }