My pickerview cannot load data from array that fetch in same view controller. But if i put fetchHistory
in another viewController(right before historyViewController) and send the data via navigation, pickerView shows confirms
data. How to make it works when i put fetchHistory
in historyViewController? or any suggest the best way to put fetchHistory
someway instead put it in random viewController?
historyViewController
var confirms = [DataWaitingConfirm]() //pickerView DataSource
override func viewDidLoad() {
super.viewDidLoad()
fetchHistory()//fetch data from server for 'confirms'
}
fetchData
func fetchHistory(){
let headers = ["Authorization" : "\(TokenType!) \(Token!)"]
Alamofire.request(.GET, "http://xxxx/history/", headers: headers, encoding: .JSON).responseJSON { response in
switch response.result {
case .Success:
if let value = response.result.value {
let json = JSON(value)
print(json)
for (results,subJson):(String, JSON) in json["results"] {
print(subJson["status"].int)
if subJson["status"].intValue == 3 {
print("ada 3")
let orderReference = subJson["px_reference"].stringValue
let date = subJson["invoice_date_str"].stringValue
let totalPaid = subJson["total_paid"].stringValue
let orderPicker = "\(orderReference) \(date) \(totalPaid)"
let confirm = DataWaitingConfirm(orderReference: orderReference, pickerConfirm: orderPicker)
self.confirms += [confirm]
}
}}
}
case .Failure(let error):
spinningActivity.hide(true)
print(error)
self.displayAlertMessage("could not fetch history")
}
}
}
pickerview delegate
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
let orderToConfirm = confirms[row]
return orderToConfirm.pickerConfirm
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return confirms.count
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
}
You need to call self.pickerView.reloadAllComponents()
to trigger the refresh. The datasource methods will then be called again and your updated data will appear in the picker view.
Your Alamofire request returns asynchronously and hence you should trigger the reload after self.confirms += confirm
.
Note: You might also consider triggering it on the main thread using dispatch_async(dispatch_get_main_queue(), {...})