swift中UIPickerView的使用

swift中UIPickerView的使用,第1张

概述https://github.com/potato512/SYSwiftLearning 示例代码如下: 1、参数定义 let width:CGFloat = (UIScreen.mainScreen().bounds.size.width / 3.0)var label:UILabel?var addressArray:Array<AnyObject>?var cityArray:Arra

https://github.com/potato512/SYSwiftLearning


示例代码如下:

1、参数定义

let wIDth:CGfloat = (UIScreen.mainScreen().bounds.size.wIDth / 3.0)var label:UILabel?var addressArray:Array<AnyObject>?var cityArray:Array<AnyObject>?var areaArray:Array<AnyObject>?    var textSelected1:String!var textSelected2:String!var textSelected3:String!

2、数据源模拟

// 数据self.loadAddress()// MARK: - 数据func loadAddress(){        // 广西        let cityArea011 = ["cityname":"南宁市","cityArea":["南宁市","隆安县","武鸣县","马山县","宾阳县","横县","芭乐县","上林县"]]        let cityArea012 = ["cityname":"百色市","cityArea":["百色市","隆林各族自治县","西林县","田林县","乐业县","凌云县","田阳县","田东县","平果县","德宝县","靖西县","那坡县"]]        let cityArea013 = ["cityname":"桂林市","cityArea":["桂林市","龙胜各簇自治县","资源县","全州县","兴安县","灵川县","灌阳县","恭城瑶族自治县","平乐县","荔浦县","阳朔县","临桂县","永福县"]]        let provinceCity01 = ["provincename":"广西省","provinceCity":[cityArea011,cityArea012,cityArea013]]        // 广东        let cityArea021 = ["cityname":"梅州市","cityArea":["梅江区","梅县","蕉岭县","大埔县","丰顺县","平远县","兴宁市"]]        let cityArea022 = ["cityname":"广州市","cityArea":["越秀","海珠","荔湾","天河","白云","黄埔","南沙","萝岗"]]        let cityArea023 = ["cityname":"深圳市","cityArea":["龙岗区","宝安区","光明新区","坪山新区","罗湖区","福田区","盐田区","南山区"]]        let cityArea024 = ["cityname":"惠州市","cityArea":["惠城区","惠东县","惠阳市","龙门县","博罗县"]]        let provinceCity02 = ["provincename":"广东省","provinceCity":[cityArea021,cityArea022,cityArea023,cityArea024]]                self.addressArray = [provinceCity01,provinceCity02]                self.cityArray = []        self.areaArray = []}

3、picker vIEw使用

3-1、初始化定义

// 实例化(注:UIPickerVIEw的宽高默认分别是屏幕宽度、216的高度)let pickervIEw = UIPickerVIEw()print(pickervIEw)self.vIEw.addSubvIEw(pickervIEw)pickervIEw.backgroundcolor = UIcolor.greencolor()        // 设置代理(注意添加代理协议,实现代理方法)pickervIEw.dataSource = selfpickervIEw.delegate = self        // 设置选择框的默认值pickervIEw.selectRow(0,inComponent:0,animated:true)pickervIEw.selectRow(0,inComponent:1,inComponent:2,animated:true)        // 刷新数据// 刷新全部数据pickervIEw.reloadAllComponents()// 刷新某列数据// pickervIEw.reloadComponent(1)

3-2、添加协议

class VIEwController: UIVIEwController,UIPickerVIEwDataSource,UIPickerVIEwDelegate {....}

3-3、代理方法实现

// MARK: - UIPickerVIEwDataSource,UIPickerVIEwDelegate// MARK: UIPickerVIEwDataSourcefunc numberOfComponentsInPickerVIEw(pickerVIEw: UIPickerVIEw) -> Int{        // 选择框列数        return 3}    func pickerVIEw(pickerVIEw: UIPickerVIEw,numberOfRowsInComponent component: Int) -> Int{        // 每个选择框的行数        if 0 == component        {            // 第一列            let count = self.addressArray!.count            return count        }        else if 1 == component        {            // 第二列            let count = self.cityArray!.count            return count        }        else if 2 == component        {            // 第三列            let count = self.areaArray!.count            return count        }                return 0}    // MARK: UIPickerVIEwDelegatefunc pickerVIEw(pickerVIEw: UIPickerVIEw,wIDthForComponent component: Int) -> CGfloat{        // 每个选择框的行宽        return wIDth}    func pickerVIEw(pickerVIEw: UIPickerVIEw,rowHeightForComponent component: Int) -> CGfloat{        // 每个选择框的行高        return 40.0}    func pickerVIEw(pickerVIEw: UIPickerVIEw,TitleForRow row: Int,forComponent component: Int) -> String?{        // 每个选择框每行的显示内容        if 0 == component        {            // 第一列            let dictProvince = self.addressArray![row]            let province:String! = dictProvince.objectForKey("provincename") as! String            return province        }        else if 1 == component        {            // 第二列            let dict = self.cityArray![row]            let city:String! = dict.objectForKey("cityname") as! String            return city        }        else if 2 == component        {            // 第三列            let area:String! = self.areaArray![row] as! String            return area        }                return nil}    func pickerVIEw(pickerVIEw: UIPickerVIEw,attributedTitleForRow row: Int,forComponent component: Int) -> NSAttributedString?{        // 每个选择框每行的显示副文本内容        return nil}    func pickerVIEw(pickerVIEw: UIPickerVIEw,vIEwForRow row: Int,forComponent component: Int,reusingVIEw vIEw: UIVIEw?) -> UIVIEw{        // 每个选择框每行自定义视图        let label = UILabel(frame: CGRectMake(0.0,0.0,wIDth,40.0))        label.backgroundcolor = UIcolor.orangecolor()        label.textAlignment = NSTextAlignment.Center                if 0 == component        {            // 第一列            let dict = self.addressArray![row]            let province:String! = dict.objectForKey("provincename") as! String            label.text = province        }        else if 1 == component        {            // 第二列            let dict = self.cityArray![row]            let city:String! = dict.objectForKey("cityname") as! String            label.text = city        }        else if 2 == component        {            // 第三列            let area:String! = self.areaArray![row] as! String            label.text = area        }                return label}    func pickerVIEw(pickerVIEw: UIPickerVIEw,dIDSelectRow row: Int,inComponent component: Int){        // 每个选择框每行被选中的值        print(component,row)          if 0 == component        {            // 第一列时的行数//            textSelected1 = String("1列 \(row) 行")                        let dict = self.addressArray![row]            textSelected1 = dict.objectForKey("provincename") as? String                        // 第二、三列关联改变            self.cityArray! = dict["provinceCity"] as! Array            pickerVIEw.reloadComponent(1)            pickerVIEw.selectRow(0,inComponent: 1,animated: true)            let dict2 = self.cityArray![0]            textSelected2 = dict2.objectForKey("cityname") as! String                        let dict3 = self.cityArray![0]            self.areaArray! = dict3["cityArea"] as! Array            pickerVIEw.reloadComponent(2)            pickerVIEw.selectRow(0,inComponent: 2,animated: true)            textSelected3 = self.areaArray![0] as! String        }        else if 1 == component        {            // 第二列时的行数//            textSelected2 = String("2列 \(row) 行")                        let dict = self.cityArray![row]            textSelected2 = dict.objectForKey("cityname") as! String                        // 第三列关联改变            self.areaArray! = dict["cityArea"] as! Array            pickerVIEw.reloadComponent(2)            pickerVIEw.selectRow(0,animated: true)            textSelected3 = self.areaArray![0] as! String        }        else if 2 == component        {            // 第三列时的行数//            textSelected3 = String("3列 \(row) 行。")                        textSelected3 = self.areaArray![row] as! String        }        let text = String("你选择了:\(textSelected1),\(textSelected2),\(textSelected3)")        self.label!.text = text}

4、显示选择结果

self.label = UILabel(frame: CGRectMake(10.0,(CGRectGetHeight(self.vIEw.bounds) - 10.0 - 40.0),(CGRectGetWIDth(self.vIEw.bounds) - 10.0 * 2),40.0))self.vIEw.addSubvIEw(self.label!)self.label!.backgroundcolor = UIcolor.yellowcolor()self.label!.textcolor = UIcolor.redcolor()self.label!.autoresizingMask = UIVIEwautoresizing.Flexibletopmargin

5、注意事项

(1)标题内容"func pickerVIEw(pickerVIEw: UIPickerVIEw,forComponent component: Int) -> String? { }"设置与自定义视图"func pickerVIEw(pickerVIEw: UIPickerVIEw,reusingVIEw vIEw: UIVIEw?) -> UIVIEw { }"不能同时设置,否则只有自定义视图有效。

总结

以上是内存溢出为你收集整理的swift中UIPickerView的使用全部内容,希望文章能够帮你解决swift中UIPickerView的使用所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1071493.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存