gpt4 book ai didi

swift - 获取所选行的索引以用于准备 segue

转载 作者:行者123 更新时间:2023-12-02 09:10:55 27 4
gpt4 key购买 nike

我想根据选择的行将不同的数组从一个 ViewController 传递到另一个 ViewController。
如何获取所选行的索引?
我已经尝试过这个,但它不起作用:

let toDetailVCSegue = "ToDetailVCSegue"

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: toDetailVCSegue, sender: indexPath)
}



func prepare(for segue: UIStoryboardSegue, sender: IndexPath?)
{
if segue.identifier == toDetailVCSegue
{
let destination = segue.destination as! DetailViewController
if let indexPath = tableView.indexPathForSelectedRow{
if indexPath.row == 0 {
destination.itemArray = namesArray
print("test")
}
if indexPath.row == 1 {
destination.itemArray = scoresArray
}
if indexPath.row == 2 {
destination.itemArray = timesArray
}
if indexPath.row == 3 {
destination.itemArray = completedArray
}

}
}
}

最佳答案

不得更改prepare(for的签名,否则它不会被调用。sender参数必须是有吗?

sender 参数转换为 IndexPath,我建议使用 switch 语句

func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
if segue.identifier == toDetailVCSegue {
let destination = segue.destination as! DetailViewController
let indexPath = sender as! IndexPath
switch indexPath.row {
case 0: destination.itemArray = namesArray
case 1: destination.itemArray = scoresArray
case 2: destination.itemArray = timesArray
case 3: destination.itemArray = completedArray
default: break
}
}
}

关于swift - 获取所选行的索引以用于准备 segue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52352336/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com