0

How to load all data into picker view, now I can load first data only this is my data from JSON​ response

**jsonResponse ==> Optional([["PlanDate": 18/01/2019, "PlanDateFullFormat": 20190118], ["PlanDateFullFormat": 20190119, "PlanDate": 19/01/2019]])

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

jsonDictionary ==>["PlanDate": 18/01/2019, "PlanDateFullFormat": 20190118]

planDate ==> 18/01/2019. ==> I want load all plant date into picker view

Loop json ==> (key: "PlanDateFullFormat", value: 20190118)

Loop json ==> (key: "PlanDate", value: 18/01/2019)**

I cannot load all data into picker view

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

2 Answers2

0

Actually its pretty easy to load data to a picker view. It's like 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
    }

}
dilaver
  • 674
  • 3
  • 17
  • yes, I have all function same you suggest, but my picker load first data only if you see JsonArray plan date have 2 value why my picker it loads first data only... –  Jun 20 '19 at 03:51
0

I'm assuming your pickerView is set up properly but you are only seeing one row? If that's the case that's probably because of this line of code:

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

You are only getting the first element of your array.

What I would do instead is just use jsonResponse directly like this:

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

Then you can use planDates to populate your pickerView.

Or maybe, you are trying to load a pickerView with data from a dictionary?

First, your ViewController has to subclass UIPickerViewDataSource, UIPickerViewDelegate.

Then in ViewDidLoad, set your ViewController as the delegate/datasource for your UIPickerView:

repeatPicker.delegate = self
repeatPicker.dataSource = self

Then implement your Delegate/Datasource methods:

   // 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? {

    }
rs7
  • 1,618
  • 1
  • 8
  • 16
  • I think this line, How to I fix this guard let jsonDictionary:Dictionary = jsonArray.first else{ return }//guard –  Jun 20 '19 at 03:50
  • You are basically getting an array of Dictionaries, so you need to treat it as such. If you use `.first`you are only getting the first element which is a Dictionary and you are missing on all the rest of the data. Use this instead: `var planDates = [Any]() if let jsonResponse = jsonResponse { for dictionary in jsonResponse { planDates.append(dictionary["PlanDate"]) } }` – rs7 Jun 20 '19 at 03:51
  • your code is worked, then if when clicking list on picker view, my label does not change with the value picker view var planDates = [Any]() if let jsonResponse = jsonResponse { for dictionary in jsonResponse {planDates.append(dictionary["PlanDate"]!) DispatchQueue.main.async { //show on pickerView self.getpPlandate = planDates as! [String] //Show on monitor self.getpPlanDatePickerView.text = planDates as! [String]}} } –  Jun 20 '19 at 04:22
  • That is probably an issue with your code inside `titleForRow`. Can you post your delegate/data source methods and especially that one? It should look something like this: `func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? { return planDates[row] }` – rs7 Jun 20 '19 at 05:05
  • this is my code titleForRow func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? { let titleRow = (getpPlandate[row]) return titleRow } –  Jun 20 '19 at 06:02