gpt4 book ai didi

swift - segue 数据传输不起作用。未显示每个角色拥有的详细数据

转载 作者:行者123 更新时间:2023-11-30 10:35:10 24 4
gpt4 key购买 nike

我尝试使用segue函数来反射(reflect) View Controller 之间的数据。单击每个字符后,应该会显示该字符的详细数据。我所看到的只是主 Storyboard中示例的 View 请给我一些建议来纠正这些代码。谢谢!

//first viewcontroller

import UIKit

class SeannyViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

let namelist = ["don", "mill", "dulu", "hem", "lue", "tine", "whip", "key"]
let bountylist = [20, 40, 70, 50, 30, 80, 90, 200]
override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showdetail" {
let vc = segue.destination as? detail_seannyViewController
if let index = sender as? Int {
vc?.name = namelist[index]
vc?.bounty = bountylist[index]
}
}
}
//how many you going to show?

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return namelist.count
}

//how you make your cell
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? ListCell else {

return UITableViewCell()
}

let img = UIImage(named: "\(namelist[indexPath.row]).jpg")
cell.imgView.image = img
cell.nameLable.text = namelist[indexPath.row]
cell.bountyLable.text = "\(bountylist[indexPath.row])"
return cell
}

// reaction after click
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("--> \(indexPath.row)")
performSegue(withIdentifier: "showDetail", sender: indexPath.row)
}
/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
}
*/

}

class ListCell: UITableViewCell {
@IBOutlet weak var imgView: UIImageView!
@IBOutlet weak var nameLable: UILabel!
@IBOutlet weak var bountyLable: UILabel!
}



// detailviewcontroller

import UIKit

class detail_seannyViewController: UIViewController {
@IBOutlet weak var imageview: UIImageView!
@IBOutlet weak var label: UILabel!
@IBOutlet weak var bountylabel: UILabel!


var name: String?
var bounty: Int?

override func viewDidLoad() {
super.viewDidLoad()
updateUI()
// Do any additional setup after loading the view.
}

func updateUI() {
if let name = self.name, let bounty = self.bounty {
let img = UIImage(named: "\(name).jpg")
imageview.image = img
label.text = name
bountylabel.text = "\(bounty)"
}
}


@IBAction func close(_ sender: UIButton) {
dismiss(animated: true, completion: nil)
}

最佳答案

确保正确设置委托(delegate)。在其他情况下,无论代码是否进入“didselectrow”方法,您都应该使用断点。请提及这一点,以便我可以提供更多帮助。

关于swift - segue 数据传输不起作用。未显示每个角色拥有的详细数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58194504/

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