我无法将数据库中的值插入选择器视图

时间:2019-06-18 04:59:42

标签: json swift uipickerview

我无法将数据库中的值插入选择器视图。
我不确定如何解决此问题。

var getpPlandate: [String] = [] 
var getpShipment: [String] = []

@IBOutlet weak var getpPlanDatePickerView: UITextField!
@IBOutlet weak var getpShipmentPickerView: UITextField!

加载数据:

func getPlanDatetoPickerview(ptruckID: String)-> Void {
        ....
        let jsonResponse = try JSONSerialization.jsonObject(with: strObject, options: []) as? [Dictionary<String,Any>]
        print("jsonResponse ==> \(String(describing: jsonResponse))")
        ....
        let planDate: String = jsonDictionary["PlanDate"] as! String
        print("planDate ==> \(planDate)")

        DispatchQueue.main.async {
            self.getpPlanDatePickerView.text = " " + planDate 
        }
    .......
    }
    task.resume()
}

选择器视图:

func numberOfComponents(在pickerView中:UIPickerView)-> Int {         返回1     }

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

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    let titleRow = (getpPlandate[row])
    return titleRow
}

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    if getpPlandate.count > 0 && getpPlandate.count >= row && getpShipment.count >= row {
        getpPlanDatePickerView.text = getpPlandate[row]
        getpShipmentPickerView.text = getpShipment[row]
        print("PlanDate ==> \(getpPlandate) - \(getpShipment)")
    }//if
}

1 个答案:

答案 0 :(得分:0)

您是否实现了UIPickerView的titleForRow或viewForRow委托方法?

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

if pickerView == getpPlanDatePickerView {
    return getpPlandate[row]
}
else if pickerView == getpShipmentPickerView {
    return getpShipment[row]
} }

另外,您的IBOutlets应该是UIPickerView而不是UITextField。