我已经声明了表格单元格,每个单元格都添加了一个标签和一个UIPicker。一旦更改了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")
}
}发布于 2017-06-07 15:11:11
使用自定义单元格和回调闭包的简单解决方案:
UILabel和UIPickerView。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)}- 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中设置数据源。
发布于 2017-06-07 15:10:44
如果需要帮助创建自定义UITableViewCell,请查看以下链接:https://www.ralfebert.de/tutorials/ios-swift-uitableviewcontroller/custom-cells/
https://stackoverflow.com/questions/44415804
复制相似问题