Ios 将数据从tableViewCell传递到另一个VC
我试图将tableViewCell的文本传递到另一个VC中,并使用该文本确定显示的图像,但无论我做什么,它都只显示最后一个单元格的文本和图像。这是我的代码,任何帮助都将不胜感激 导入UIKit 类执行库:UIViewController、UITableViewDataSource、UITableViewDelegate{Ios 将数据从tableViewCell传递到另一个VC,ios,swift,Ios,Swift,我试图将tableViewCell的文本传递到另一个VC中,并使用该文本确定显示的图像,但无论我做什么,它都只显示最后一个单元格的文本和图像。这是我的代码,任何帮助都将不胜感激 导入UIKit 类执行库:UIViewController、UITableViewDataSource、UITableViewDelegate{ override func viewDidLoad() { super.viewDidLoad() exerciseTableView.delegate =
override func viewDidLoad() {
super.viewDidLoad()
exerciseTableView.delegate = self
exerciseTableView.dataSource = self
}
var exercises = ExerciseLibrary()
var mainCell = UITableViewCell()
@IBOutlet weak var exerciseTableView: UITableView!
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return exercises.exerciseArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "prototypeCell2", for: indexPath)
cell.textLabel?.text = exercises.exerciseArray[indexPath.row]
cell.layer.borderWidth = 5
cell.layer.cornerRadius = 20
cell.layer.borderColor = #colorLiteral(red: 0, green: 0.3285208941, blue: 0.5748849511, alpha: 1)
cell.textLabel?.textColor = UIColor.black
cell.textLabel?.adjustsFontSizeToFitWidth = true
cell.textLabel?.font = .boldSystemFont(ofSize: 15)
return cell
}
}
在下一个VC中,无论单击哪个单元格,它始终显示肩部按压和肩部按压图像
通过故事板中的ctrl-drag方法设置segue
多谢各位
乔希你需要
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.performSegue(withIdentifier:"segue",sender:exercises.exerciseArray[indexPath.row])
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let item = sender as! String
let displayVC = segue.destination as! Exercise
if item == "Bench press" {
displayVC.navTitle = "Bench Press"
displayVC.image = UIImage(named: "BenchPress")!
}
else
if item == "Barbell squat" {
displayVC.navTitle = "Barbell Squat"
displayVC.image = UIImage(named: "BBSquat")!
}
else
if item == "Shoulder press" {
displayVC.navTitle = "Shoulder Press"
displayVC.image = UIImage(named: "BBShoulderPress")!
}
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.performSegue(withIdentifier:"segue",sender:exercises.exerciseArray[indexPath.row])
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let item = sender as! String
let displayVC = segue.destination as! Exercise
if item == "Bench press" {
displayVC.navTitle = "Bench Press"
displayVC.image = UIImage(named: "BenchPress")!
}
else
if item == "Barbell squat" {
displayVC.navTitle = "Barbell Squat"
displayVC.image = UIImage(named: "BBSquat")!
}
else
if item == "Shoulder press" {
displayVC.navTitle = "Shoulder Press"
displayVC.image = UIImage(named: "BBShoulderPress")!
}
}