简体   繁体   English

如何获取UIPicker的更改值以用于标签? -斯威夫特3

[英]How to get the changed value of UIPicker to be used for a Label? - Swift-3

I have declared table cells and each cell I have added a label and a UIPicker. 我已经声明了表单元格,并且每个单元格都添加了一个标签和一个UIPicker。 Once the UIPicker Value is changed, I would like to show the value in the label of the corresponding label. 一旦更改了UIPicker值,我想在相应标签的标签中显示该值。

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")
    }
}

Simple solution using a custom cell and a callback closure: 使用自定义单元格和回调闭包的简单解决方案:

  • Create a custom cell in Interface Builder, add an UILabel and an UIPickerView . 在Interface Builder中创建一个自定义单元,添加一个UILabel和一个UIPickerView
  • Create a custom class PickerViewCell , which contains the outlets, the picker data source and a callback closure. 创建一个自定义类PickerViewCell ,其中包含出口,选择器数据源和回调闭包。

     class PickerViewCell: UITableViewCell { var pickerDataSource = ["Alpha", "Beta", "Gamma", "Delta"] var callback : ((String) -> ())? @IBOutlet var picker : UIPickerView! @IBOutlet var selectedText : UILabel! } extension PickerViewCell : UIPickerViewDataSource, UIPickerViewDelegate { func numberOfComponents(in pickerView: UIPickerView) -> Int { return 1 } func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { return pickerDataSource.count } func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? { return pickerDataSource[row] } func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { callback?(pickerDataSource[row]) } } 
  • In Interface Builder 在Interface Builder中

    • Set the class of the custom cell to PickerViewCell and the identifier to PickerCell . 将自定义单元格的类设置为PickerViewCell ,将标识符设置为PickerCell
    • Connect the UI elements to the outlets. 将UI元素连接到插座。
    • Connect delegate and datasource of the picker to the custom cell. 将选取器的委托和数据源连接到自定义单元。
  • Implement cellForRow this way, the callback is used to update the label text and to be able to update the data model and do other things after the user picks a value. 以这种方式实现cellForRow ,回调将用于更新标签文本,并能够在用户选择值后更新数据模型和执行其他操作。

     override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "PickerCell", for: indexPath) as! PickerViewCell cell.callback = { (value) in cell.selectedText.text = value // update the data model } return cell } 

If the picker instances are supposed to have different data source arrays, declare the arrays in the view controller and set the data source in cellForRow respectively. 如果应该选择器实例具有不同的数据源数组,请在视图控制器中声明该数组,然后分别在cellForRow设置数据源。

  1. Create a custom UITableViewCell. 创建一个自定义UITableViewCell。
  2. Add a UILabel and UIPickerView to it. 向其中添加一个UILabel和UIPickerView。
  3. Set the custom cell as the delegate for the picker view and move the pickers methods to the custom cell. 将自定义单元格设置为Picker视图的委托,并将Pickers方法移至自定义单元格。
  4. In the didSelectRow function update the cells corresponding UILabel 在didSelectRow函数中,更新与UILabel相对应的单元格

Have a look at the following link if you need help creating a custom UITableViewCell: https://www.ralfebert.de/tutorials/ios-swift-uitableviewcontroller/custom-cells/ 如果需要创建自定义UITableViewCell的帮助,请查看以下链接: https : //www.ralfebert.de/tutorials/ios-swift-uitableviewcontroller/custom-cells/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在快速单击按钮时获取 tableview 标签值 - how can I get tableview label value on button click in swift 如何将soap对象传递给一个viewcontroller到Swift-3中的另一个视图控制器 - how can i pass soap object to one viewcontroller to another view controller in Swift-3 UILabel Swift-3中显示的额外行 - Extra line shown in UILabel Swift-3 Swift-3错误:' - [_ SwiftValue unsignedIntegerValue]:无法识别的选择器 - Swift-3 error: '-[_SwiftValue unsignedIntegerValue]: unrecognized selector 更改一个组件时如何读取uipicker多个组件 - How to read uipicker multiple component when one component is changed 将从UIPicker中选择的值分配给Swift中的UITextField(以及如何显示和隐藏它) - Assigning value chosen from a UIPicker to a UITextField in Swift (also how to show and hide it) 如何在 UIPicker 的两列之间添加标签? - How would you add a label between two columns in a UIPicker? Swift:使用 UIPicker 自定义 DatePicker - Swift: Custom DatePicker with UIPicker 滚动后表格视图单元格标签更改并在其中获得错误的值该如何解决? - Table view cell label get changed after scrolling and get wrong value in it how to solve it? iOS Swift:如何将图像放置在动态更改的标签的左侧 - iOS Swift: How to put an image to the left of a dynamically changed label
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM