[英]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: 使用自定义单元格和回调闭包的简单解决方案:
UILabel
and an UIPickerView
. 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中
PickerViewCell
and the identifier to PickerCell
. PickerViewCell
,将标识符设置为PickerCell
。 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
设置数据源。
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.