在标签或文本中显示下拉菜单值



我已经挣扎了一段时间了。我试着在Xcode中创建一个下拉菜单,我已经完成了,但我的主要目标是在一个标签或类似的东西中显示一些东西,例如:我从菜单中选择"公寓1";然后在UILabel中显示价格"$ 2000">

代码如下:

@IBOutlet weak var purchaseprice: UILabel!

@IBOutlet weak var btnSelectPropert: UIButton!
let transparentView = UIView()
let tableView = UITableView()
var selectedButton = UIButton()

var dataSource = [String]()


override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
tableView.register(CellClass.self, forCellReuseIdentifier: "Cell")

}
func addTransparentView(frames: CGRect) {
let window = UIApplication.shared.keyWindow
transparentView.frame = window?.frame ?? self.view.frame
self.view.addSubview(transparentView)

tableView.frame = CGRect(x: frames.origin.x, y: frames.origin.y + frames.height, width: frames.width, height: 400)
self.view.addSubview(tableView)
tableView.layer.cornerRadius = 2

transparentView.backgroundColor = UIColor.black.withAlphaComponent(0.9)
tableView.reloadData()
let tapgesture = UITapGestureRecognizer(target: self, action: #selector(removeTransparentView))
transparentView.addGestureRecognizer(tapgesture)
transparentView.alpha = 0

UIView.animate(withDuration: 0.4, delay: 0.0, usingSpringWithDamping: 1.0, initialSpringVelocity: 1.0, options: .curveEaseInOut,animations: {
self.transparentView.alpha = 0.5
self.tableView.frame = CGRect(x: frames.origin.x, y: frames.origin.y + frames.height + 5, width: frames.width, height: CGFloat(self.dataSource.count * 50))
}, completion: nil)

}
@objc  func removeTransparentView() {
let frames = selectedButton.frame
UIView.animate(withDuration: 0.4, delay: 0.0, usingSpringWithDamping: 1.0, initialSpringVelocity: 1.0, options: .curveEaseInOut,animations: {
self.transparentView.alpha = 0
self.tableView.frame = CGRect(x: frames.origin.x, y: frames.origin.y + frames.height, width: frames.width, height: 0)
}, completion: nil)
}
@IBAction func onClickSelectProperty(_ sender: Any) {
dataSource = ["Apartment A3","Apartment A4","Apartment A5","Apartment A6","Apartment A8","Apartment A9","Apartment A12","Apartment E1","Sectional Title 50A","Sectional Title 65A","Free Standing 70A","Free Standing 70A Core","Free Standing 85A","Free Standing 100A"]
selectedButton = btnSelectPropert
addTransparentView(frames: btnSelectPropert.frame)
self.comfee.text = "N$: (btnSelectPropert)"
}
extension CalculatorViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dataSource.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
cell.textLabel?.text = dataSource[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 50
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
selectedButton .setTitle(dataSource[indexPath.row], for: .normal)
removeTransparentView()
}

我已经设法让菜单工作,但我不知道如何显示一个值在uilabel

将此代码添加到didSelectRowAt方法

switch dataSource[indexPath.row] {
case "Apartment A3":
purchaseprice.text = "2000"
case "Apartment A4":
purchaseprice.text = "3000"
case "Apartment A5":
purchaseprice.text = "4000"
case "Apartment A6":
purchaseprice.text = "5000"
case "Apartment A7":
purchaseprice.text = "6000"
default :
break
}