Ios 选择器视图未更新
由于一些问题,我尝试使用Alamofire,这里我有一个Alamofire,它获取一个在线JSON文件,获取四个结果,将它们放在picker视图中,并刷新,以便它们可以显示。这里的代码非常有效:Ios 选择器视图未更新,ios,json,swift,Ios,Json,Swift,由于一些问题,我尝试使用Alamofire,这里我有一个Alamofire,它获取一个在线JSON文件,获取四个结果,将它们放在picker视图中,并刷新,以便它们可以显示。这里的代码非常有效: Alamofire.request(.GET, "https://example.com/json.json", parameters: ["foo": "bar"]) .responseJSON { response in print(resp
Alamofire.request(.GET, "https://example.com/json.json", parameters: ["foo": "bar"])
.responseJSON { response in
print(response.request) // original URL request
print(response.response) // URL response
print(response.data) // server data
print(response.result) // result of response serialization
if let JSON = response.result.value {
self.mypickerview.delegate = self
self.mypickerview.dataSource = self
self.pickerData = [JSON["One"] as! String, JSON["Two"] as! String, JSON["Three"] as! String, JSON["Four"] as! String]
self.mypickerview.delegate = self;
}
}
但问题是,这段代码虽然成功地获取了数据,但没有显示在picker视图中:
// Setup the session to make REST GET call. Notice the URL is https NOT http!!
let postEndpoint: String = "https://example.com/json.json"
let session = NSURLSession.sharedSession()
let url = NSURL(string: postEndpoint)!
// Make the POST call and handle it in a completion handler
session.dataTaskWithURL(url, completionHandler: { ( data: NSData?, response: NSURLResponse?, error: NSError?) -> Void in
// Make sure we get an OK response
guard let realResponse = response as? NSHTTPURLResponse where
realResponse.statusCode == 200 else {
print("Not a 200 response")
return
}
// Read the JSON
do {
if let ipString = NSString(data:data!, encoding: NSUTF8StringEncoding) {
// Print what we got from the call
print(ipString)
// Parse the JSON to get the data
let jsonDictionary = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as! NSDictionary
let One = jsonDictionary["One"] as! String
let Two = jsonDictionary["Two"] as! String
let Three = jsonDictionary["One"] as! String
let Four = jsonDictionary["One"] as! String
self.mypickerview.delegate = self
self.mypickerview.dataSource = self
self.pickerData = [One, Two, Three, Four]
self.mypickerview.delegate = self;
}
} catch {
print("bad things happened")
}
}).resume()
第二个代码可能会出现什么问题,阻止选择器视图中的选项显示?之后
self.pickerData = [One, Two, Three, Four]
调用以下命令以强制重新加载主线程中的数据,如注释中提到的@SaintThread
self.mypickerview.reloadAllComponents()
当您需要更改pickerView时-调用此方法
self.mypickerview.reloadAllComponents()
你确定你在排队吗?