gpt4 book ai didi

ios - Swift didSelectRowAt 将数组值传递给第二个 View

转载 作者:行者123 更新时间:2023-11-28 06:20:27 24 4
gpt4 key购买 nike

我正在寻找解决将数据从第一个 View Controller 转移到第二个 View Controller 时遇到的问题的想法。当用户选择表格单元格时,将调用第二个 View Controller 。

填充第一个表格 View 的代码

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = mtgRates.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! ViewControllerTableViewCell

cell.fiName.text = fetchedFiName[indexPath.row].fiName
cell.oneYear.text = fetchedFiName[indexPath.row].oneYear
cell.twoYear.text = fetchedFiName[indexPath.row].twoYear
cell.threeYear.text = fetchedFiName[indexPath.row].threeYear
cell.fourYear.text = fetchedFiName[indexPath.row].fourYear
cell.fiveYear.text = fetchedFiName[indexPath.row].fiveYear


return (cell)

}

我看过很多 youtube 视频,但在使用全局数组集设置数据时,它们都采用了一种简单的方法。

我一直在工作但目前什么也没做的代码。

 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {


oneYearFound = self.fetchedFiName[indexPath.row].oneYear
twoYearFound = self.fetchedFiName[indexPath.row].twoYear
threeYearFound = self.fetchedFiName[indexPath.row].threeYear
fourYearFound = self.fetchedFiName[indexPath.row].fourYear
fiveYearFound = self.fetchedFiName[indexPath.row].fiveYear

performSegue(withIdentifier: "segue", sender: self)



}

我认为我的问题是将获取的结果发送到第二个 View Controller

感谢您的帮助!


更多信息基于回复。你是对的,我在 Storyboard上确实有两个 View Controller 。到目前为止,我的 UIViewController 的代码是

class SegueViewController: UIViewController {

@IBOutlet weak var V2TwoYear: UILabel!
@IBOutlet weak var V2FiveYear: UILabel!
@IBOutlet weak var V2FourYear: UILabel!
@IBOutlet weak var V2ThreeYear: UILabel!
@IBOutlet weak var V2OneYear: UILabel!
@IBOutlet weak var V2FiName: UILabel!

override func viewDidLoad() {

super.viewDidLoad()

V2FiName.text = foundFi[myIndex].fiName
V2TwoYear.text = foundFi[myIndex].twoYear
V2OneYear.text = foundFi[myIndex].oneYear
V2ThreeYear.text = foundFi[myIndex].threeYear
V2FourYear.text = foundFi[myIndex].fourYear
V2FiName.text = foundFi[myIndex].fiveYear

}

最佳答案

为什么不传递一个 fetchedFiName 的实例?

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let selectedFiName = self.fetchedFiName[indexPath.row]
performSegue(withIdentifier: "segue", sender: selectedFiName)
}

然后,将您的发件人转换为 YOUR_FETCHED_FI_NAME_CLASS 并将其传递给 prepareForSegue 中的目标 View Controller :

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

if let selectedFiName = sender as? YOUR_FETCHED_FI_NAME_CLASS,
destVC = segue.destination as? SegueViewController {
destVC.passedFiName = selectedFiName
}
}

然后,在您的目标 viewController 中和 ViewDidLoad 之后(因为您的标签不会在此之前加载)您可以使用您的 passedFiName 来填充您的标签。

override func viewDidLoad() {
super.viewDidLoad()
updateLabels()
}

func updateLabels() {
V2FiName.text = passedFiName.fiName
V2TwoYear.text = passedFiName.twoYear
V2OneYear.text = passedFiName.oneYear
V2ThreeYear.text = passedFiName.threeYear
V2FourYear.text = passedFiName.fourYear
V2FiName.text = passedFiName.fiveYear
}

更新:

我仍然对此有疑问。我想我越来越近了

Controller 一代码

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {


let selectedFiName = self.fetchedFiName[indexPath.row].fiName
let selectedOneYear = self.fetchedFiName[indexPath.row].oneYear
let selectedTwoYear = self.fetchedFiName[indexPath.row].twoYear
let selectedThreeYear = self.fetchedFiName[indexPath.row].threeYear
let selectedFourYear = self.fetchedFiName[indexPath.row].fourYear
let selectedFiveYear = self.fetchedFiName[indexPath.row].fiveYear

passData = [SecondTable(passedFIName: selectedFiName, passedOneYear: selectedOneYear, passedTwoYear: selectedTwoYear, passedThreeYear: selectedThreeYear, passedFourYear: selectedFourYear, passedFiveYear: selectedFiveYear)]


performSegue(withIdentifier: "SecondViewController", sender: self)


}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

if let selectedFiName = sender as! ,
let destVC = segue.destination as? SecondViewController {
destVC.fiName = selectedFiName
}
}

第二 View Controller 代码`

struct SecondTable {

var passedFIName: String = ""
var passedOneYear: String = ""
var passedTwoYear: String = ""
var passedThreeYear: String = ""
var passedFourYear: String = ""
var passedFiveYear: String = ""

类 SecondViewController: UIViewController {

@IBOutlet weak var fiName: UILabel!
@IBOutlet weak var sometext: UILabel!

override func viewDidLoad() {
super.viewDidLoad()

let fiDetails = SecondTable()

fiName.text = .passedFIName
sometext.text = "Some Text"


}

我在 `override func prepare(for segue: UIStoryboardSegue, sender: Any?) 收到错误消息 我不确定在“发件人作为”缺失值“

我已经搜索了几个小时。一我解决了这个问题。我的项目将开始进行。感谢您提供的所有帮助!`

关于ios - Swift didSelectRowAt 将数组值传递给第二个 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43750060/

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