学习笔记:UIPickerView

学习笔记:UIPickerView

利用UIPickerView控件实现省市区三级联动的效果,如下图:
这里写图片描述

代码是转载http://www.hangge.com/blog/cache/detail_1169.html
,稍有修改

代码如下:

import UIKit

class ViewController: UIViewController,UIPickerViewDataSource,UIPickerViewDelegate {

    @IBOutlet weak var picker: UIPickerView!

    @IBOutlet weak var btn: UIButton!


    //所以地址数据集合
    var addressArray = [[String: AnyObject]]()
    //var addressArray:NSArray!

    //选择的省索引
    var provinceIndex = 0
    //选择的市索引
    var cityIndex = 0
    //选择的县索引
    var areaIndex = 0

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        let path = NSBundle.mainBundle().pathForResource("address", ofType:"plist")

        addressArray = NSArray(contentsOfFile: path!) as! Array

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    //设置选择器的列数
    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
        return 3
    }

    //设置选择器的行数
    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        if component == 0 {
            return self.addressArray.count
        } else if component == 1 {
            let province = self.addressArray[provinceIndex]
            return province["cities"]!.count
        } else {
            let province = self.addressArray[provinceIndex]
            if let city = province["cities"]![cityIndex] as? [String: AnyObject] {
                return city["areas"]!.count
            } else {
                return 0
            }
        }
    }

    //设置选择框各选项的内容,继承于UIPickerViewDelegate协议
    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int)
        -> String? {
            if component == 0 {
                return self.addressArray[row]["state"] as? String
            }else if component == 1 {
                let province = self.addressArray[provinceIndex]
                return province["cities"]![row]["city"] as? String
            }else {
                let province = self.addressArray[provinceIndex]
                let city = province["cities"]![cityIndex] as? [String: AnyObject]
                return city!["areas"]![row] as? String
            }
    }

    //选中项改变事件(将在滑动停止后触发)
    func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        //根据列、行索引判断需要改变数据的区域
        switch (component) {
        case 0:
            provinceIndex = row;
            cityIndex = 0;
            areaIndex = 0;
            pickerView.reloadComponent(1);
            pickerView.reloadComponent(2);
            pickerView.selectRow(0, inComponent: 1, animated: false);
            pickerView.selectRow(0, inComponent: 2, animated: false);
        case 1:
            cityIndex = row;
            areaIndex = 0;
            pickerView.reloadComponent(2);
            pickerView.selectRow(0, inComponent: 2, animated: false);
        case 2:
            areaIndex = row;
        default:
            break;
        }
    }

    //触摸按钮时,获得被选中的索引
    @IBAction func getPickerViewValue(){
        //获取选中的省
        let p = self.addressArray[provinceIndex]
        let province = p["state"]!

        //获取选中的市
        let c = p["cities"]![cityIndex]
        let city = c["city"] as! String

        //获取选中的县(地区)
        var area = ""
        if (c["areas"] as! [String]).count > 0 {
            area = (c["areas"] as! [String])[areaIndex]
        }

        //拼接输出消息
        let message = "索引:\(provinceIndex)-\(cityIndex)-\(areaIndex)\n"
            + "值:\(province) - \(city) - \(area)"

        //消息显示
        let alertController = UIAlertController(title: "您选择了",
                                                message: message, preferredStyle: .Alert)
        let cancelAction = UIAlertAction(title: "确定", style: .Cancel, handler: nil)
        alertController.addAction(cancelAction)
        self.presentViewController(alertController, animated: true, completion: nil)
    }


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值