0

I am trying to change the button and its function depending on the cell that is tapped. Currently, I have a list of hospitals that the user will see. The clicked cell will open up a detail consisting of two labels, an image and a button. Everything works as typed except the button. I would like it to change per cell to match the phone number of the given hospital. Any ideas?

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

2 Answers2

1

Declare a struct for managing hospital details and pass or directly assign using the method prepareforsegue . Here is the modified version of your Code @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()
    }

}

Change the "segue identifier" as per your requirement

Vinodh
  • 5,262
  • 4
  • 38
  • 68
0

First of all you have to declare prepareSegue method in PhoneBookTableViewController after tableView method

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

(please add more detail here)

user230910
  • 2,353
  • 2
  • 28
  • 50
Khyati Modi
  • 630
  • 1
  • 9
  • 19
  • Hi Khyati and welcome to Stack Overflow. Please can you add a little more detail to your answer? I'd like to know how this fixes the problem, and what it actually does? – user230910 Jul 08 '19 at 04:43
  • But that won't allow me to have several phone numbers. My goal is to have approximately 15 different phone numbers, one for each cell. – Wesley Bryant Jul 08 '19 at 04:43
  • in didSelectAtRowmethod you pass the phoneNo of perticular Selected Row and the time of use that passed data you can assign that phoneNo to your cell's property for example: cell.phoneNum = phoneNo – Khyati Modi Jul 08 '19 at 05:26