ios - 想要从下拉列表中选择选项并根据所选选项更改按钮标题...如何?

标签 ios iphone swift swift3

对于下拉菜单,当我从下拉列表中选择选项时,我使用 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/

相关文章:

ios - 与 Android 相比,UIWebView 加载页面的时间太长?

ios - Swift函数错误: 'Float' is not convertible to 'Void'

使用相同类型约束的 Swift 协议(protocol)与关联类型的一致性

iOS/Xcode : Group overlapping elements in Stack View

javascript - 滚动时点击 Phonegap 移动应用程序会选择不正确的项目

iPhone - 将 UIImage 保存到模拟器上的桌面

iOS swift : Class property with implicity unwrapped optional vs without

ios - 如何比较两幅图像并突出差异?

iphone - 如何在单个存储库中维护多个iOS应用程序?

iphone - ViewDidLoad 方法不保留变量?