gpt4 book ai didi

ios - 准备在 for 循环内进行 segue

转载 作者:搜寻专家 更新时间:2023-11-01 05:54:37 26 4
gpt4 key购买 nike

我有一个包含以下 willSelectRowAt 代码的 TableView :

func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
for n in 0...carsArray.count - 1 {
if indexPath.row == n {
print(carsArray[n].name)
func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "goToEditCar" {
let name = carsArray[n].name
print(name)
let indexCar = n

let destinationVC = segue.destination as! EditCarViewController
destinationVC.name = name
destinationVC.indexCar = indexCar
}
}

performSegue(withIdentifier: "goToEditCar", sender: self)
}
}
}

不知何故 prepare 函数不会传递所需的数据,print(name) 也不会 - 谁能告诉我这 block 的问题代码?

最佳答案

您的代码根本无法运行。您使用了错误的 API,prepare(for 永远不会在另一个方法中调用,实际上您不需要循环。

willSelectRowAt 是控制一个单元格是否允许被选中。如果允许则返回 indexPath 否则返回 nil

这不是你想要的。使用 didSelect 并在调用 performSegue

时将索引路径作为 sender 传递
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: "goToEditCar", sender: indexPath)
}

prepare(for中从sender参数中获取索引路径

func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "goToEditCar" {
let indexPath = sender as! IndexPath
let name = carsArray[indexPath.row].name
print(name)

let destinationVC = segue.destination as! EditCarViewController
destinationVC.name = name
destinationVC.indexCar = indexPath.row
}
}

关于ios - 准备在 for 循环内进行 segue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56225729/

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