如何使选择器视图从 swift 3 中的特定行显示?
Posted
技术标签:
【中文标题】如何使选择器视图从 swift 3 中的特定行显示?【英文标题】:How to make picker view to display from a particular row in swift 3? 【发布时间】: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")
【问题讨论】:
Swift: How to set a default Value of a UIPickerView with three components in Swift? 和 ***.com/questions/11777072/… 可能重复 但它仍然从第一个国家本身打开选择器视图@SandeepBhandari 我想知道您从我提供的重复参考中修改了您的代码中的哪些内容。请使用您从上面发布的参考资料中尝试的内容更新您的问题。如果仍有问题,我们将很乐意为您提供帮助。我可以发布答案,但重复答案对社区没有任何好处,因此避免这样做 我在视图中添加了这段代码确实加载了countryPickerView.selectRow(37, inComponent: 0, animated: true)
,当我点击国家文本字段时,国家选择器视图已经打开,但这里显示的第一个国家是Afghanistan
,但不是United States
我需要它是United States
但不是Afghanistan
在 viewDidLoad 中添加代码将不起作用,因为到那时您的 countryPickerView 还没有数据:) 再想一想,您就有答案了:D
【参考方案1】:
有类变量来跟踪选择。它应该由您的选择器委托函数func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
更新。而不是在 viewDidLoad() 中更新,您将不得不更新 func viewWillAppear(_ animated: Bool)
中的选择,以便每次更新值。 viewDidLoad() 仅在第一次加载视图时执行,而不是每次访问时执行。
在 swift 4 中,它看起来像这样:
//class variable
var selectedCountry:Int = 0
var column : Int = 0
// Catpure the picker view selection
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
selectedCountry = row
column = component
override func viewWillAppear(_ animated: Bool)
super.viewWillAppear(animated);
countryPickerView.selectRow(selectedCountry, inComponent: column, animated: true))
【讨论】:
【参考方案2】:首先你需要知道什么是美国的指数。因此,当您调用 api 时,将其索引存储在变量 selectedCountry 中。之后调用选择器委托方法 selectedRow。
var selectedCountry:Int = 0
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 itemNumber in 0..<jsonObj!.count
let dict = Country(dict:jsonObj![itemNumber])
if dict.countryName == "United States"
self.selectedCountry = itemNumber
self.countryPickerView.selectRow(self.selectedCountry, inComponent: 0, animated: true)
self.countryArray.append(dict)
print(self.countryArray)
OperationQueue.main.addOperation(
self.countryPickerView.reloadAllComponents()
self.statePickerView.reloadAllComponents()
)
).resume()
希望这会有所帮助。
【讨论】:
我已经静态给出了加载代码,即像这样countryPickerView.selectRow(37, inComponent: 0, animated: true)
,但选择器视图是从第一个国家本身加载而不是从传递的值
首先得到响应后,你需要调用 self.countryPickerView.reloadAllComponents 然后设置 countryPickerView.selectRow(37, inComponent: 0, animated: true)【参考方案3】:
您可以使用ActionSheetPicker,这实际上减少了所有选择器方法的代码,并且您可以使用其他条件在选择器视图中将您选择的国家/地区作为默认值
if txtCountry.text == ""
let countryPicker = ActionSheetStringPicker(title: "Select a country", rows: pickerData, initialSelection: 0, doneBlock: (pickerSheet, selectedIndex, selectedValue) in
self.txtCountry.text = selectedValue! as? String
self.selectedCountry = selectedIndex
, cancel: pickerSheet in return , origin: (sender as AnyObject).superview!)
countryPicker?.show();
else
let countryPicker = ActionSheetStringPicker(title: "Select a country", rows: pickerData, initialSelection: selectedCountry, doneBlock: (pickerSheet, selectedIndex, selectedValue) in
self.txtCountry.text = selectedValue! as? String
self.selectedCountry = selectedIndex
, cancel: pickerSheet in return , origin: (sender as AnyObject).superview!)
countryPicker?.show();
这里self.selectedCountry = selectedIndex
帮你动态管理 selectedCountry Index
【讨论】:
@User 让我知道这个答案是否对您有帮助 不,我只需要用正常的选择器视图来做,不要去其他图书馆,谢谢@D.Desai的回复以上是关于如何使选择器视图从 swift 3 中的特定行显示?的主要内容,如果未能解决你的问题,请参考以下文章