如何将所有数据加载到选择器视图中

How to load all data into picker view

如何将所有数据加载到选择器视图中,现在我只能加载第一个数据 这是我来自 JSON 响应

的数据

**json响应 ==> 可选([["PlanDate": 18/01/2019, "PlanDateFullFormat": 20190118], ["PlanDateFullFormat": 20190119, "PlanDate":2019 年 1 月 19 日]])

json数组==>[["PlanDate": 18/01/2019, "PlanDateFullFormat": 20190118], ["PlanDateFullFormat": 20190119, "PlanDate": 19/01/2019]]

json字典 ==>["PlanDate": 18/01/2019, "PlanDateFullFormat": 20190118]

计划日期 ==> 18/01/2019。 ==> 我想将所有工厂日期加载到选择器视图中

循环json ==>(键:"PlanDateFullFormat",值:20190118)

循环json ==>(键:"PlanDate",值:18/01/2019)**

我无法将所有数据加载到选择器视图中

func getPlanDatetoPickerview(ptruckID: String)-> Void {
        .....

                //check data shipment for json Dictionary
                let planDate: String = jsonDictionary["PlanDate"] as! String
                print("planDate ==> \(planDate)")

                //show on pickerView
                for myplanDate in jsonDictionary{
                    print("Loop json ==> \(myplanDate)")
                }//for
                self.getpPlandate = [jsonDictionary["PlanDate"] as! String]
                .....
            }catch let myerror{
                print(myerror)
                //          check display plandate in database
               ....
                }//DispatchQueue
            }//catch
        }//task
        task.resume()
    }//getPlanDatetoPickerview

实际上,将数据加载到选择器视图非常容易。就像 UITableView.


class YourViewController {

    var yourArray: [YourObject] = []
    var yourPicker = UIPicker()

    override func viewDidLoad() {
        super.viewDidLoad()

        yourPicker.dataSource = self
        yourPicker.delegate = self
    }

}

// MARK: - UIPickerViewDataSource

extension YourViewController: UIPickerViewDataSource {

    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }

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

}

// MARK: - UIPickerViewDelegate

extension YourViewController: UIPickerViewDelegate {

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return yourArray[row].title
    }

}

我假设您的 pickerView 设置正确,但您只看到一行?如果是这样,那可能是因为这行代码:

//change array to dictionary
guard let jsonDictionary:Dictionary = jsonArray.first else{
     return
}//guard
print("jsonDictionary ==>\(jsonDictionary)")

您只获取数组的第一个元素。

我会做的只是像这样直接使用 jsonResponse:

var planDates = [Any]()
if let jsonResponse = jsonResponse {
   for dictionary in jsonResponse {
      planDates.append(dictionary["PlanDate"])
   }
}

然后您可以使用 planDates 来填充您的 pickerView。

或者,您可能正在尝试使用字典中的数据加载 pickerView?

首先,您的 ViewController 必须继承 UIPickerViewDataSource, UIPickerViewDelegate

然后在 ViewDidLoad 中,将您的 ViewController 设置为您的 UIPickerView 的 delegate/datasource:

repeatPicker.delegate = self
repeatPicker.dataSource = self

然后实施您的 Delegate/Datasource 方法:

   // The number of components of your UIPickerView
   func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }

    // The number of rows of data
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        // Return number of planDate entries
    }

    // The data to return for the row and component (column) that's being passed in
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {

    }