gpt4 book ai didi

ios - 更改 UITableView 中每个单元格的 UIButton

转载 作者:行者123 更新时间:2023-11-28 13:34:04 25 4
gpt4 key购买 nike

我正在尝试根据点击的单元格更改按钮及其功能。目前,我有一个用户将看到的医院列表。单击的单元格将打开一个由两个标签、一个图像和一个按钮组成的详细信息。除按钮外,一切都按键入的方式工作。我希望每个单元格都发生变化,以匹配给定医院的电话号码。有什么想法吗?

import UIKit
var hospital = ["Mercy Medical Center"]
var hospitalDesc = ["Roseburg"]
var myIndex = 0

class PhoneBookTableViewController: UITableViewController {

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return hospital.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "PBCell", for: indexPath)

cell.textLabel?.text = hospital[indexPath.row]
cell.detailTextLabel?.text = hospitalDesc[indexPath.row]

return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
myIndex = indexPath.row
performSegue(withIdentifier: "segue", sender: self)
}
}


class PhoneBookDetail: UIViewController{

@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var descLabel: UILabel!
@IBOutlet weak var myImageView: UIImageView!
@IBOutlet weak var callHospital: UIButton!

let phoneVA = "tel://5412175034"

override func viewDidLoad() {
super.viewDidLoad()

titleLabel.text = hospital[myIndex]
descLabel.text = hospitalDesc[myIndex]
myImageView.image = UIImage(named: hospital[myIndex] + ".jpg")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}

最佳答案

声明一个用于管理医院详细信息的结构,并使用 prepareforsegue 方法传递或直接赋值。这是您的代码@Wesley Bryant 的修改版本

struct Hospital{
var name = ""
var location = ""
var contact = ""
var image = ""
}


class PhoneBookTableViewController : UITableViewController{

var hospitals :[Hospital] = [Hospital]()
var myIndex = 0

override func viewDidLoad() {
super.viewDidLoad()

hospitals.append(Hospital(name: "Mercy Medical Center", location: "Roseburg", contact: "tel://5412175034", image: "0.jpg"))

}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return hospitals.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "PBCell", for: indexPath)
let hospital = hospitals[indexPath.row]
cell.textLabel?.text = hospital.name
cell.detailTextLabel?.text = hospital.location

return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
myIndex = indexPath.row
performSegue(withIdentifier: "Detail", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "Detail" {
let destinationVC = segue.destination as! PhoneBookDetail
let hospital = hospitals[myIndex]
destinationVC.selectedHospital = hospital
//OR
destinationVC.callHospital.setTitle(hospital.contact, for: UIControl.State.normal)
destinationVC.titleLabel.text = hospital.name
destinationVC.descLabel.text = hospital.location
destinationVC.myImageView.image = UIImage(named: hospital.image)
}
}

}


class PhoneBookDetail: UIViewController{
var selectedHospital : Hospital = Hospital()

@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var descLabel: UILabel!
@IBOutlet weak var myImageView: UIImageView!
@IBOutlet weak var callHospital: UIButton!



override func viewDidLoad() {
super.viewDidLoad()

titleLabel.text = selectedHospital.name
descLabel.text = selectedHospital.location
myImageView.image = UIImage(named: selectedHospital.image)

let phoneVa = selectedHospital.contact
print(phoneVa)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}

}

根据您的要求更改“segue 标识符”

关于ios - 更改 UITableView 中每个单元格的 UIButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56928296/

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