学习笔记: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)
}
}