如果触摸了单元格 - 继续移动另一个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
}
}
}
当我触摸每个细胞时 - 它不会移动。 请帮忙
答案 0 :(得分:1)
点击一个单元格时,有两个选项可以执行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
}
}
答案 1 :(得分:0)
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
}
}
答案 2 :(得分:-1)
修改强>
@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)
}