简体   繁体   English

pickerView无法加载在同一视图控制器(Swift)中获取的数据

[英]pickerView can't load data that fetch in same view controller (swift)

My pickerview cannot load data from array that fetch in same view controller. 我的pickerview无法从同一视图控制器中提取的数组中加载数据。 But if i put fetchHistory in another viewController(right before historyViewController) and send the data via navigation, pickerView shows confirms data. 但是,如果我将fetchHistory放在另一个viewController中(在historyViewController之前)并通过导航发送数据,pickerView将显示confirms数据。 How to make it works when i put fetchHistory in historyViewController? 当我将fetchHistory放入fetchHistory时,如何使其工作? or any suggest the best way to put fetchHistory someway instead put it in random viewController? 或任何建议最好的方式放置fetchHistory而不是将其放在随机的viewController中?

historyViewController historyViewController

var confirms = [DataWaitingConfirm]() //pickerView DataSource
override func viewDidLoad() {
    super.viewDidLoad()

    fetchHistory()//fetch data from server for 'confirms'
}

fetchData 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 pickerview代表

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. 您需要调用self.pickerView.reloadAllComponents()来触发刷新。 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 . 您的Alamofire请求异步返回,因此您应该在self.confirms += confirm Confirm之后触发重新加载。

Note: You might also consider triggering it on the main thread using dispatch_async(dispatch_get_main_queue(), {...}) 注意:您也可以考虑使用dispatch_async(dispatch_get_main_queue(), {...})在主线程上触发它

You should use after ws response: 您应该在ws响应后使用:

reloadComponent: reloadComponent:

or 要么

reloadAllComponents: reloadAllComponents:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM