如果单元格被触摸 - 移动到另一个 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 从 TableView 单元格连接到目标 Controller
在本例中,单元格作为
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 从源 Controller 连接到目标 Controller
在这种情况下,您必须实现
didSelectRowAt
,调用performSegue(withIdentifier
)并将索引路径作为sender
参数传递func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { performSegue(withIdentifier: "transData", sender: indexPath) }
然后
准备(因为
必须是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 } }
关于ios - 如果单元格被触摸 - 移动到另一个 VC [SWIFT],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44996910/