------------------------------------------------------------------------------------------
// // ViewController.swift // UIPickerViewSample // // Created by jinnchang on 15/4/2. // Copyright (c) 2015年 Jinn Chang. All rights reserved. // import UIKit class ViewController: UIViewController,UIPickerViewDelegate,UIPickerViewDataSource { var pickerView: UIPickerView! var hourLength = 24 var minuteLength = 60 override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view,typically from a nib. pickerView = UIPickerView(frame: CGRectMake(0,self.view.frame.size.height - 400,self.view.frame.size.width,400)) pickerView.delegate = self pickerView.dataSource = self self.view.addSubview(pickerView) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } // 设置个数 func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int { return 2 } // 设置行数 func pickerView(pickerView: UIPickerView,numberOfRowsInComponent component: Int) -> Int { if(component == 0){ return hourLength } if(component == 1){ return minuteLength } return 0 } // 设置每行具体内容 func pickerView(pickerView: UIPickerView,titleForRow row: Int,forComponent component: Int) -> String! { if(component == 0){ return "\(row)时" } if(component == 1){ return "\(row)分" } return nil } // 选中行的操作 func pickerView(pickerView: UIPickerView,didSelectRow row: Int,inComponent component: Int) { if(component == 0){ println("hour selected: \(row)") } if(component == 1){ println("minute selected: \(row)") } } }------------------------------------------------------------------------------------------
GitHub 上项目地址:UIPickerViewSample
控件更多相关属性及方法参考:UIPickerView Class Reference 文章最后更新时间:2015年4月3日11:15:03