jsonswiftxcode10.2

How to load all data into picker view


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

Solution

  • 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? {
    
        }