class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
//UIView: UITableView
let pickerView = UIPickerView(frame: CGRect(x: 0, y: 100, width: self.view.bounds.size.width, height: 200))
pickerView.dataSource = self
pickerView.delegate = self
self.view.addSubview(pickerView)
//UIControl
let datePicker = UIDatePicker(frame: CGRect(x: 0, y: 300, width: self.view.bounds.size.width, height: 200))
// datePicker.datePickerMode = .Date
datePicker.datePickerMode = .Time
datePicker.addTarget(self, action: #selector(didChange(_:)), forControlEvents: .ValueChanged)
self.view.addSubview(datePicker)
}
func didChange(sender: UIDatePicker) {
print(sender.date) //NSDate
}
//列数
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 3
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return 4
}
// func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
// return "a"
// }
//可以重用
func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView?) -> UIView {
if let v = view {
print("reusing")
v.backgroundColor = UIColor.redColor()
return v
}
else {
print("no reuse")
let greenView = UIView()
greenView.backgroundColor = UIColor.greenColor()
return greenView
}
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
print("\(component): \(row)")
}
}
PickerView、DatePicker
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 废话不多说,直接上代码 代码很简单,适用于UIPickerView跟DatePicker,还有其他的,有兴趣的朋友...
- 前言 喔时间是一种很奇妙的东西,数字亦如是。在Android开发中,肯定会有一些需求是针对于选择器的处理,甚至会有...
- func pickerView(_ pickerView: UIPickerView, viewForRow ro...