I have been struggling for a while now. I'm trying to create a Dropdown Menu in Xcode which I've done, but my main goal is to display something in a uilabel or something similar, for Example: I select from the menu "Apartment 1" then in UILabel it displays the price "$ 2000"
The code is as follows:
@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()
}
I've managed to get menu working but I don't know how to display a value in uilabel
CodePudding user response:
Add the this code in didSelectRowAt method
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
}