【问题标题】:Change UIButton per cell in UITableView在 UITableView 中更改每个单元格的 UIButton
【发布时间】:2019-11-17 13:51:57
【问题描述】:

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

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

【问题讨论】:

  • 对于“callHospital”,viewDidLoad下没有数据分配,您可以检查一下您的代码吗?
  • 那是因为我不确定如何分配这个。
  • 你能检查一下吗? stackoverflow.com/questions/26326296/…
  • 这不是我想要完成的。我想为每个单元格拨打不同的号码。
  • 好的。数据是动态的还是静态的?

标签: ios swift xcode uitableview uibutton


【解决方案1】:

声明一个用于管理医院详细信息的结构体,并使用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 标识符”

【讨论】:

  • 除按钮外一切正常。它只是说按钮,不会在屏幕上打印数字,按下时也不会工作。
  • 你在说什么减号?
  • 没有减号按钮?除了通话按钮,一切正常。
【解决方案2】:

首先你要在PhoneBookTableViewController中的tableView方法之后声明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
        }
    }

(请在此处添加更多详细信息)

【讨论】:

  • 嗨,Khyati,欢迎来到 Stack Overflow。请问您可以在答案中添加更多细节吗?我想知道这是如何解决问题的,以及它的实际作用是什么?
  • 但这不允许我拥有多个电话号码。我的目标是拥有大约 15 个不同的电话号码,每个单元一个。
  • 在 didSelectAtRow 方法中,您传递特定选定行的 phoneNo 和传递数据的使用时间,您可以将该 phoneNo 分配给您的单元格的属性,例如:cell.phoneNum = phoneNo
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多