論 Swift 開發入門 : 選取器(UIPickerView)

來源:互聯網
上載者:User

標籤:ios   swift   選取器   uipickerview   

轉載請聲明出處:http://blog.csdn.net/jinnchang/article/details/44487269
------------------------------------------------------------------------------------------
1、Summary
------------------------------------------------------------------------------------------
2、Code
////  ViewController.swift//  UIPickerViewSample////  Created by jinnchang on 15/3/18.//  Copyright (c) 2015年 Jinn Chang. All rights reserved.//import UIKitclass 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        }    }    }
------------------------------------------------------------------------------------------
3、ResourceGithub 上項目地址:UIPickerViewSample

文章最後更新時間:。參考資料如下:

UIPickerView Class Reference

UIKit User Interface Catalog: Picker Views

論 Swift 開發入門 : 選取器(UIPickerView)

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.