首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将更改后的UIPicker值用于标签?- Swift-3

如何将更改后的UIPicker值用于标签?- Swift-3
EN

Stack Overflow用户
提问于 2017-06-07 14:41:32
回答 2查看 856关注 0票数 0

我已经声明了表格单元格,每个单元格都添加了一个标签和一个UIPicker。一旦更改了UIPicker值,我希望在相应标签的标签中显示该值。

代码语言:javascript
复制
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UIPickerViewDelegate, UIPickerViewDataSource {

    @IBOutlet weak var myTable: UITableView!


    override func viewDidLoad() {
        super.viewDidLoad()
    }


    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
        return 2
    }


    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{

        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)

        let picker = UIPickerView()
        let y = 200 * indexPath.row
        picker.frame = CGRect(x: 200, y: y, width: 100, height: 150)
        picker.delegate = self
        picker.dataSource = self
        picker.tag = indexPath.row
        view.addSubview(picker)

        let myLbl = UILabel()
        myLbl.frame = CGRect(x: 100, y: y, width: 80, height: 100)
        myLbl.backgroundColor = UIColor.gray
        myLbl.textColor = UIColor.white
        myLbl.textAlignment = .center
        myLbl.tag = indexPath.row
        myLbl.text = "hi" // I would like to show the value of the UI picker
        view.addSubview(myLbl)

        return cell
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 200
    }


    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print(indexPath.row)
        NotificationCenter.default.addObserver(self, selector: #selector(pickerChangeAction), name: NSNotification.Name("pickerChange"), object: self)
    }

    public func numberOfComponents(in pickerView: UIPickerView) -> Int{
        return 1
    }


    public func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
        return myData.count
    }

     public func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?{

        return myData[row]

}

     public func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int){

       NotificationCenter.default.post(name: NSNotification.Name("pickerChange"), object: self)
        print("Hello2")
    }

    func pickerChangeAction(){
        print("Hello1")
    }
}
EN

回答 2

Stack Overflow用户

发布于 2017-06-07 15:11:11

使用自定义单元格和回调闭包的简单解决方案:

  • 在Interface中创建自定义单元,添加UILabelUIPickerView
  • 创建一个自定义类PickerViewCell,其中包含出入口、选择器数据源和回调闭包。 类PickerViewCell: UITableViewCell { var pickerDataSource = "Alpha“、"Beta”、"Gamma“、"Delta”var回调:((字符串) -> ())?@IBOutlet var选择器: UIPickerView!@IBOutlet var selectedText : UILabel!} extension : UIPickerViewDataSource,UIPickerViewDelegate { func numberOfComponents(in pickerView: UIPickerView) -> Int {返回1} func pickerView(_ pickerView: UIPickerView,pickerView组件: Int) Int {返回}c(_:,行: Int,forComponent组件: Int) ->字符串?{返回pickerDataSourcerow } func pickerView(_ pickerView: UIPickerView,didSelectRow行: Int,inComponent component: Int) {回调?(PickerDataSourcerow)}
  • 接口生成器
代码语言:javascript
复制
- Set the class of the custom cell to `PickerViewCell` and the identifier to `PickerCell`.
- Connect the UI elements to the outlets.
- Connect delegate and datasource of the picker to the custom cell.

  • 以这种方式实现cellForRow,回调将用于更新标签文本,并能够在用户选择值之后更新数据模型和做其他事情。 重写func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier:"PickerCell",for: indexPath) as!PickerViewCell cell.callback ={(值) in cell.selectedText.text =cell.selectedText.text //更新数据模型}返回单元}

如果选择器实例应该有不同的数据源数组,则分别在视图控制器中声明数组并在cellForRow中设置数据源。

票数 1
EN

Stack Overflow用户

发布于 2017-06-07 15:10:44

  1. 创建一个自定义UITableViewCell。
  2. 向它添加一个UILabel和UIPickerView。
  3. 将自定义单元格设置为选择器视图的委托,并将选择器方法移动到自定义单元格。
  4. 在didSelectRow函数中更新相应的单元格( UILabel )

如果需要帮助创建自定义UITableViewCell,请查看以下链接:https://www.ralfebert.de/tutorials/ios-swift-uitableviewcontroller/custom-cells/

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44415804

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档