【发布时间】:2017-12-06 06:13:50
【问题描述】:
如何使选择器视图从默认国家/地区显示,但它从第一个国家/地区开始显示,但我需要它来自默认国家/地区 united states 但这里它从第一个国家/地区显示,但我需要它从美国显示任何人都可以帮我如何实现这个?
这里是从第一个国家/地区显示的选择器视图下面显示的图像,但我需要它从美国显示,因为它是我默认选择的国家/地区
func countryListDownloadJsonwithURL(countryAPI: String) {
let url = NSURL(string: countryAPI)
URLSession.shared.dataTask(with: (url as URL?)!, completionHandler: {(data, response, error) -> Void in
if let jsonObj = try? JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [[String:Any]] {
for item in jsonObj! {
let dict = Country(dict: item)
self.countryArray.append(dict)
}
print(self.countryArray)
OperationQueue.main.addOperation({
self.countryPickerView.selectRow(self.i, inComponent: 0, animated: true)
self.countryPickerView.reloadAllComponents()
self.statePickerView.reloadAllComponents()
})
}
}).resume()
}
func numberOfComponents(in pickerView: UIPickerView) -> Int{
if pickerView.tag == 1 {
return 1
}else {
return 1
}
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
if pickerView.tag == 1 {
return countryArray.count
}
else {
return countryArray[countrySelectedIndex!].state.count
}
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if pickerView.tag == 1 {
return countryArray[row].country
}
else {
return countryArray[countrySelectedIndex!].state[row].state
}
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerView.tag == 1 {
countryTextField.text = countryArray[row].country
for item in countryArray {
print(item.country)
print(countryArray[row].country)
if item.country == countryArray[row].country {
self.countryId = item.countryId
if item.state.count == 0 {
self.stateTextfield.isHidden = false
self.stateLabel.isHidden = true
self.selectedCountryTextField.isHidden = true
self.imageArrow.isHidden = true
break
}
else {
if let i = countryArray.index(where: { $0.country == countryArray[row].country }) {
countrySelectedIndex = i
}
self.countryId = item.countryId
if self.countryId?.isEmpty == true {
self.countryId = countryTextField.text
}
UserDefaults.standard.set(self.countryId, forKey: "guestCountryId")
self.statePickerView.delegate = self
self.statePickerView.dataSource = self
selectedCountryTextField.inputView = statePickerView
self.statePickerView.reloadAllComponents()
selectedCountryTextField.text = "Select state/province"
self.stateLabel.isHidden = false
self.stateTextfield.isHidden = true
self.selectedCountryTextField.isHidden = false
self.imageArrow.isHidden = false
break
}
}
else {
self.stateTextfield.isHidden = false
self.stateLabel.isHidden = true
self.selectedCountryTextField.isHidden = true
self.imageArrow.isHidden = true
}
}
}
else {
self.selectedCountryTextField.text = countryArray[countrySelectedIndex!].state[row].state
self.stateCode = countryArray[countrySelectedIndex!].state[row].stateValue as! Int
print(self.stateCode)
UserDefaults.standard.set(self.stateCode, forKey: "statecode")
}
}
【问题讨论】:
-
但它仍然从第一个国家本身打开选择器视图@SandeepBhandari
-
我想知道您从我提供的重复参考中修改了您的代码中的哪些内容。请使用您从上面发布的参考资料中尝试的内容更新您的问题。如果仍有问题,我们将很乐意为您提供帮助。我可以发布答案,但重复答案对社区没有任何好处,因此避免这样做
-
我在视图中添加了这段代码确实加载了
countryPickerView.selectRow(37, inComponent: 0, animated: true),当我点击国家文本字段时,国家选择器视图已经打开,但这里显示的第一个国家是Afghanistan,但不是United States我需要它是United States但不是Afghanistan -
在 viewDidLoad 中添加代码将不起作用,因为到那时您的 countryPickerView 还没有数据:) 再想一想,您就有答案了:D
标签: ios swift3 uipickerview