对于下拉菜单,当我从下拉列表中选择选项时,我使用 UItableView 和按钮来显示下拉列表,我想根据所选选项更改按钮的标题? ??
//点击食物按钮
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return array.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "foodcell", for: indexPath)
// cell.textLabel?.text = array [indexPath.row]
cell.textLabel?.text = array [indexPath.row]
return cell
}
@nonobjc func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
let cell = tableView.cellForRow(at: indexPath)
selectFoodbutton.setTitle(cell?.textLabel?.text, for: .normal)
self.foodtable.isHidden = true
}
@IBAction func selectFoodbutton(_ sender: Any) {
self.foodtable.isHidden = !self.foodtable.isHidden
}
最佳答案
在按钮操作中编写代码以显示表格 View ,如下所示。
首先,您在该按钮下方创建一个表格 View 。 将数据源加载到该 TableView 中。 然后选择单元格之一。 然后将按钮标题设置为选定的标题。 然后最后隐藏表格 View 。
像这样的代码片段
@IBAction func educationBtnAction(_ sender: AnyObject) {
// let point = textfield.getBottomTableViewPoint()
let point = sender.getPointBased(level: 1)
self.educationTableview = UITableView(frame: CGRect(x: point.x, y: point.y, width: sender.frame.size.width + 10, height: 150), style: .plain)
self.educationTableview.dataSource = self
self.educationTableview.delegate = self
self.educationTableview.backgroundColor = UIColor.clear
self.educationTableview.applyPropertiesToPopupTableView()
// textfield.superview?.addSubview(labPanelTableView)
bgScrollView.addSubview(educationTableview)
if self.educationTableview.alpha == 0.0{
UIView.animate(withDuration: 0.5, delay: 0.0, options: .curveEaseOut, animations: {
self.hidePopups()
self.educationTableview.alpha = 1.0
}, completion: { finished in
})
}
commonTypeArray = ["1", "2", "3", "4", "5",]
self.educationTableview.reloadData()
}
func hidePopups() {
self.educationTableview.alpha = 0.0
}
对于剩余的过程,上面的代码就足够了
关于ios - 想要从下拉列表中选择选项并根据所选选项更改按钮标题...如何?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46565124/