【问题标题】:list of countries into uipickerview swift 3进入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.
    }
}

【问题讨论】:

    标签: arrays swift3 uipickerview


    【解决方案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]
    }
    

    【讨论】:

      猜你喜欢
      • 2014-08-10
      • 2014-12-19
      • 1970-01-01
      • 2023-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-10
      • 1970-01-01
      相关资源
      最近更新 更多