在我的应用程序中,我有一个选择器,在POST请求后,它会填充来自我服务器的数据。
运行时,屏幕上似乎什么也没有发生,选择器没有改变。但一旦点击,新数据就会立即出现,并正常工作。看起来数据源更改工作正常,但直到刷新后显示才会更改。
有没有办法刷新它,或者有其他方法来更改数据源
以下是相关代码:
class MyController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
@IBOutlet weak var dossiers_picker: UIPickerView!
var pickerData: [Dossier] = [Dossier(nom: "Dossier", id: 0)]
// Dossier is just a tuple class with 2 attributes
override func viewDidLoad() {
super.viewDidLoad()
self.dossiers_picker.delegate = self
self.dossiers_picker.dataSource = self
getDossiers()
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return pickerData.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return pickerData[row].nom
}
func getDossiers() {
... // Cut the code where I call the server to get the data, etc ...
let task = session.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else {
// check for fundamental networking error
print("error=\(String(describing: error))")
return
}
... // get the data and turn it into an array of Dossier classes in var dossiers
self.pickerData = dossiers
dossiers_picker.reloadAllComponents()
}
task.resume()
}
}