PikerView 在我的 View Controller 中没有显示数据。
当我使用一些 Array 文字值对其进行测试时,它工作正常。
我应该在某处重新加载数据吗?
class FirstViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource
{
@IBOutlet var baseCurrency: UITextField!
@IBOutlet var excahngeCurrency: UITextField!
@IBOutlet var dropDown: UIPickerView!
var list: [String: AnyObject] = [String: AnyObject]()
var titles: [String] = []
override func viewDidLoad()
{
super.viewDidLoad()
let url:URL = URL(string: "https://openexchangerates.org/api/currencies.json?app_id=7486951aeb994599a08cd7979bef3c03")!
Alamofire.request(url).responseJSON(completionHandler:
{
response in
switch response.result
{
case.success:
self.list = response.result.value as! [String: AnyObject]
for (key, _) in self.list
{
self.titles += [key]
}
print(self.titles)
case.failure:
print("error")
}
})
}
func numberOfComponents(in pickerView: UIPickerView) -> Int
{
return 1
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?
{
return self.titles[row]
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
{
return self.titles.count
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
{
exchangeCurrency.text = self.titles[row]
}
}
Best Answer-推荐答案 strong>
如果您确实从 URL 获取数据(我没有验证这一点,并且我假设您确实在 print 语句中看到了数据)您需要调用 reloadAllComponents 在您的选择器 View 上,以便在获取数据后重新加载数据。
关于ios - pickerview 不显示数组中的数据,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/42881554/
|