标签:ios swift 选择器 uipickerview
转载请声明出处:http://blog.csdn.net/jinnchang/article/details/44487269// // ViewController.swift // UIPickerViewSample // // Created by jinnchang on 15/3/18. // Copyright (c) 2015年 Jinn Chang. All rights reserved. // import UIKit class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource { var myPickerView: UIPickerView? var provinces = [String: [String]]() var cities = [String]() override func viewDidLoad() { // 载入数据 provinces = ["浙江省":["杭州市","宁波市"],"安徽省":["黄山市","合肥市"]] cities = provinces.values.array[0] // 定义一个按钮,使数据回到默认状态 let button1 = UIButton.buttonWithType(.System) as? UIButton button1?.frame = CGRectMake(self.view.frame.width/2 - 200, 50, 400, 50) button1?.setTitle("回到默认状态", forState: UIControlState.Normal) button1?.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside) button1?.tag = 1 // 定义一个按钮,刷新所有数据 let button2 = UIButton.buttonWithType(.System) as? UIButton button2?.frame = CGRectMake(self.view.frame.width/2 - 200, 150, 400, 50) button2?.setTitle("刷新所有元素", forState: UIControlState.Normal) button2?.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside) button2?.tag = 2 // 定义一个按钮,显示当前选中的省市 let button3 = UIButton.buttonWithType(.System) as? UIButton button3?.frame = CGRectMake(self.view.frame.width/2 - 200, 250, 400, 50) button3?.setTitle("显示当前选中省市", forState: UIControlState.Normal) button3?.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside) button3?.tag = 3 // 初始化 myPickerView myPickerView = UIPickerView(frame: CGRectMake(0, self.view.frame.height - 200, self.view.frame.width, 200)) myPickerView?.delegate = self myPickerView?.dataSource = self // 显示选中框,iOS7 以后不起作用 myPickerView?.showsSelectionIndicator = false self.view.addSubview(button1!) self.view.addSubview(button2!) self.view.addSubview(button3!) self.view.addSubview(myPickerView!) } // 设置列数 func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int { return 2 } // 设置行数 func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { if(component == 0){ return provinces.keys.array.count } if(component == 1){ return provinces.count } return 0 } // 设置每行具体内容(titleForRow 和 viewForRow 二者实现其一即可) func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! { if(component == 0){ return provinces.keys.array[row] } if(component == 1){ return cities[row] } return nil } // 选中行的操作 func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { if(component == 0){ cities = provinces[provinces.keys.array[row]]! // 重新加载二级选项并复位 myPickerView?.reloadComponent(1) myPickerView?.selectRow(0, inComponent: 1, animated: true) } } /// 响应按钮点击事件 func buttonAction(sender: UIButton) { let num = sender.tag switch num { case 1: // 复原数据并重置 provinces = ["浙江省":["杭州市","宁波市"],"安徽省":["黄山市","合肥市"]] cities = provinces.values.array[0] myPickerView?.reloadAllComponents() myPickerView?.selectRow(0, inComponent: 0, animated: true) myPickerView?.selectRow(0, inComponent: 1, animated: true) case 2: // 载入新数据并重置 provinces = ["浙江":["杭州","宁波"],"安徽":["黄山","合肥"]] cities = provinces.values.array[0] myPickerView?.reloadAllComponents() myPickerView?.selectRow(0, inComponent: 0, animated: true) myPickerView?.selectRow(0, inComponent: 1, animated: true) case 3: // 显示当前选中的省市 let provinceNum = myPickerView?.selectedRowInComponent(0) let cityNum = myPickerView?.selectedRowInComponent(1) println("province:\(provinces.keys.array[provinceNum!]);city:\(cities[cityNum!])") default: break } } }------------------------------------------------------------------------------------------
文章最后更新时间:。参考资料如下:
UIKit
User Interface Catalog: Picker Views
论 Swift 开发入门 : 选择器(UIPickerView)
标签:ios swift 选择器 uipickerview
原文地址:http://blog.csdn.net/jinnchang/article/details/44487269