进入uipickerview swift 3的国家列表
Posted
技术标签:
【中文标题】进入uipickerview swift 3的国家列表【英文标题】:list of countries into uipickerview swift 3 【发布时间】:2016-11-25 04:42:07 【问题描述】:我在 Google 上搜索了如何找到国家/地区列表并尝试将其实施到 PickerView
,但我被困在我尝试执行 countries.count
的这一点上,它给了我一个错误提示:
无法将“int”类型的返回表达式转换为“字符串”类型 有什么建议吗?
import UIKit
class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate
@IBOutlet weak var pickerValue: UIPickerView!
let countries = NSLocale.isoCountryCodes.map (code:String) -> String in
let id = NSLocale.localeIdentifier(fromComponents: [NSLocale.Key.countryCode.rawValue: code])
return NSLocale(localeIdentifier: "en_US").displayName(forKey: NSLocale.Key.identifier, value: id) ?? "Country not found for code: \(code)"
func numberOfComponents(in pickerView: UIPickerView) -> Int
return 1
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?
return countries.count
override func viewDidLoad()
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
override func didReceiveMemoryWarning()
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
【问题讨论】:
【参考方案1】:您需要在方法numberOfRowsInComponent
中返回数组计数,在titleForRow
中您需要返回数组对象,表明它将显示在选择器中。
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
return countries.count
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?
return countries[row]
【讨论】:
以上是关于进入uipickerview swift 3的国家列表的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 Swift 3 UIPickerView 引用多行?