我试图根据所点击的单元格来更改按钮及其功能。目前,我有用户可以看到的医院列表。单击的单元格将打开一个包含两个标签,图像和按钮的详细信息。除按钮外,所有内容均按键入方式工作。我希望更改每个单元格以匹配给定医院的电话号码。有什么想法吗?
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()
}
}
答案 0 :(得分:1)
声明一个用于管理医院详细信息的结构,并使用方法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()
}
}
通过或直接分配。这是您的代码@Wesley Bryant的修改版本
{{1}}
根据您的要求更改“ segue标识符”
答案 1 :(得分:0)
首先,您必须在tableView方法之后在PhoneBookTableViewController中声明prepareSegue方法
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "segue" {
//write the name of view controller where you want to show the details
let destinationVC = segue.destination as! ViewController
//make one global variable in viewController where you want to show the details and set that variable in this prepare method
//here phoneNo is global variable of viewController where you want to show the data
destinationVC.phoneNo. = phoneVA
destinationVC.delegate = self
}
}
(请在此处添加更多详细信息)