如果单元格被触摸 - 移动到另一个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
参数传递override 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
并将索引路径作为参数传递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)
}