我有带名称和ID的json数据。即时通讯使用pickerview查看json名称。如果用户选择了json的名称,则该名称将出现在textfield。但是问题是我只需要发送ID的名称 不在服务器参数中命名
这是我的代码
var Officerlist = [DistrictNames]()
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return Officerlist.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return Officerlist[row].userName
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
let DistrictsName = Officerlist[row].userName
DistrictUnit.text = DistrictsName
return
}
func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
var label: UILabel
if let view = view as? UILabel {
label = view
}else {
label = UILabel()
}
label.textColor = UIColor.red
label.textAlignment = .center
label.font = UIFont(name: "Menlo-Regular", size: 18)
label.text = Officerlist[row].userName
return label
}
let parameter = ["officerId": DistrictUnit.text!]
答案 0 :(得分:0)
您将通过params[:product]
方法获得ID
didSelectRow
还有一件事是var index = Int()
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
let districtsName = Officerlist[row].userName
index = row // It gives selected row index
index = Officerlist[row].id // It gives selected row Id
DistrictUnit.text = districtsName
return
}
应该以小写字母开头。
答案 1 :(得分:0)
在方法之外创建对象,例如:
var selectedOfficer: DistrictNames?
然后当用户在您的row
方法中选择任何didSelectRow
时保存它,例如:
selectedOfficer = Officerlist[row]
您的方法将如下所示:
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
let DistrictsName = Officerlist[row].userName
DistrictUnit.text = DistrictsName
selectedOfficer = Officerlist[row]
}
现在,当您创建parameter
时,您可以像这样获得它:
if let selectedOfficeObj = selectedOfficer {
let parameter = ["officerId": selectedOfficeObj.id]
}