加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

数组 – 进入uipickerview swift 3的国家列表

发布时间:2020-12-14 05:33:37 所属栏目:百科 来源:网络整理
导读:我用Google搜索了如何查找国家/地区的列表并尝试实施到PickerView,但是我在这一点上陷入困??境,我尝试做country.count它给了我一个错误说: cannot convert return expression of type ‘int’ to return type ‘string’ any suggestions? import UIKitclas
我用Google搜索了如何查找国家/地区的列表并尝试实施到PickerView,但是我在这一点上陷入困??境,我尝试做country.count它给了我一个错误说:

cannot convert return expression of type ‘int’ to return type ‘string’
any suggestions?

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.
    }
}
您需要在方法numberOfRowsInComponent中返回数组计数,并且在titleForRow中需要返回数组对象,显示它将在picker中显示.
func pickerView(_ pickerView: UIPickerView,numberOfRowsInComponent component: Int) -> Int {
    return countries.count
}

func pickerView(_ pickerView: UIPickerView,forComponent component: Int) -> String? {
    return countries[row]
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读