Segue数据传输不起作用.每个字符都有的详细数据没有显示



我尝试使用 segue 函数来反映视图控制器之间的数据。 单击每个字符后,它应该显示其中的详细数据。 我看到的只是主情节提要中示例的视图 请给我一些建议来纠正此代码。谢谢!

//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)
}

确保正确设置委托。在另一种情况下,无论代码是否在"didselectrow"方法中,您都应该使用断点。请提及这一点,以便我提供更多帮助。

区分大小写很重要。

didSelectRowAt"showDetail"prepare(for segue中的"showdetail"

不同

旁注:

不要guardcellForRow中的单元格。强制向下投射单元格。如果代码崩溃,它会揭示一个可以立即修复的设计错误。

let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! ListCell

最新更新