-4

I've a function, getSemainesStages() which read a distant json file and put the result in a (global scope) array.

The problem is that it's asynchronous like somebody explain to me and I need this array to populate a pickerView datasource.

So when I call:

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

The array is still empty, and the same for

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

How can I do that only after my getSemainesStages() function is empty ?

shallowThought
  • 19,212
  • 9
  • 65
  • 112
  • Please do not ask multiple questions [for one topic](http://stackoverflow.com/questions/43184215/json-and-variable-scope) and [search](http://stackoverflow.com/a/3206956/1457385) before posting a question. – shallowThought Apr 03 '17 at 14:05

1 Answers1

0

You can call UIPickerView.reloadAllComponents() after the array has been populated to reload the sections/rows:

var lesSemaines: [String] {
    didSet { 
        pickerView.reloadAllComponents()
    }
}
Xavier Lowmiller
  • 1,381
  • 1
  • 15
  • 24