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

标签 ios swift xcode uitableview uibutton

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

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/

相关文章:

iOS UITableView : Empty header and footer height?

ios - 可以多次调用 UNUserNotificationCenter.current().requestAuthorization() 吗?

ios - Collectionview 在快速选择新行时取消选择所选行

ios - 存档应用程序时 Images.xcassets 中的图像不显示?

iphone - 恢复自动续订订阅的订阅购买

ios - 如何将一个 View 的顶部设置为另一个 View 的底部

ios - iOS中如何判断 "TapEnded"

ios - 使用 DatePicker 和 TextField 时将 String 转换为 NSDate

iOS慢速图像像素迭代

ios xcode9.0 swift4.0 MQTT - 如何使用证书连接到MQTT服务器?