Swift - 选择框(UIPickerView)的用法
用法 选择 swift
2023-09-11 14:17:59 时间
1,选择框可以让用户以滑动的方式选择值。示例如下:
2,调整选择框的尺寸
UIPickerView用frame和center两个属性设置整个选择框的大小和位置。
如果要调整内部列的宽度,需要实现UIPickerViewDelegate协议类中pickerView:widthForComponent方法设置
如果要调整内部行高,则需要实习上述协议类中pickerView:rowHeightForComponent方法设置
3,将图片作为选择框选项
选择框选项的内容,除了可以使字符串类型的,还可以是任意UIView类型的元素。比如我们将选项内容设置为图片:
4,检测响应选项的选择状态
![](http://www.hangge.com/blog_uploads/201504/201504151452029961.png)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
import UIKit class ViewController : UIViewController , UIPickerViewDelegate , UIPickerViewDataSource { var pickerView: UIPickerView ! override func viewDidLoad() { super .viewDidLoad() pickerView= UIPickerView () //将dataSource设置成自己 pickerView.dataSource= self //将delegate设置成自己 pickerView.delegate= self //设置选择框的默认值 pickerView.selectRow(1,inComponent:0,animated: true ) pickerView.selectRow(2,inComponent:1,animated: true ) pickerView.selectRow(3,inComponent:2,animated: true ) self .view.addSubview(pickerView) //建立一个按钮,触摸按钮时获得选择框被选择的索引 var button= UIButton (frame: CGRectMake (0,0,100,30)) button.center= self .view.center button.backgroundColor= UIColor .blueColor() button.setTitle( "获取信息" ,forState:. Normal ) button.addTarget( self , action: "getPickerViewValue" , forControlEvents: UIControlEvents . TouchUpInside ) self .view.addSubview(button) } //设置选择框的列数为3列,继承于UIPickerViewDataSource协议 func numberOfComponentsInPickerView( pickerView: UIPickerView ) -> Int { return 3 } //设置选择框的行数为9行,继承于UIPickerViewDataSource协议 func pickerView(pickerView: UIPickerView ,numberOfRowsInComponent component: Int ) -> Int { return 9 } //设置选择框各选项的内容,继承于UIPickerViewDelegate协议 func pickerView(pickerView: UIPickerView !,titleForRow row: Int ,forComponent component: Int ) -> String !{ return String (row)+ "-" + String (component) } //触摸按钮时,获得被选中的索引 func getPickerViewValue(){ var alertView= UIAlertView (); alertView.title= "被选中的索引为" alertView.message= String (pickerView.selectedRowInComponent(0))+ "-" + String (pickerView!.selectedRowInComponent(1))+ "-" + String (pickerView.selectedRowInComponent(2)) alertView.addButtonWithTitle( "OK" ) alertView.show() } } |
UIPickerView用frame和center两个属性设置整个选择框的大小和位置。
如果要调整内部列的宽度,需要实现UIPickerViewDelegate协议类中pickerView:widthForComponent方法设置
如果要调整内部行高,则需要实习上述协议类中pickerView:rowHeightForComponent方法设置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
//设置列宽 func pickerView(pickerView: UIPickerView !,widthForComponent component: Int ) -> CGFloat { if (00 == component){ //第一列变宽 return 100 } else { //第二、三列变窄 return 30 } } //设置行高 func pickerView(pickerView: UIPickerView !,rowHeightForComponent component: Int ) -> CGFloat { return 50 } |
选择框选项的内容,除了可以使字符串类型的,还可以是任意UIView类型的元素。比如我们将选项内容设置为图片:
1
2
3
4
5
6
7
|
func pickerView(pickerView: UIPickerView !,viewForRow row: Int ,forComponent component: Int , reusingView view: UIView !) -> UIView !{ var image = UIImage (named: "icon_" + String (row)) var imageView = UIImageView () imageView.image = image return imageView } |
1
2
3
4
5
|
func pickerView(pickerView: UIPickerView !,didSelectRow row: Int , inComponent component: Int ){ //将在滑动停止后触发,并打印出选中列和行索引 println (component) println (row) } |
相关文章
- C#中Dictionary的用法及用途
- PHP switch的“高级”用法详解
- nginx的location、root、alias指令用法和区别
- Python排序算法之选择排序定义与用法示例
- php数据库连接mysql与mysqli的区别与用法
- Generator 函数的含义与用法
- ABAP Webdynpro Interface View的用法
- TypeScript 里 never 类型的用法举例
- C# MessageBox用法实例 (附效果图)
- Python语言学习之字母S开头函数使用集锦:set/sys/super用法之详细攻略
- C++之auto与decltype用法区别(一百一二十五)
- 【自动化测试 】你应该学会的Postman用法
- ButterKnife注入注解框架用法
- HTML DOM中innerHTML、textContent和innerText属性的用法与区别
- ffmpeg avpicture_fill用法
- 把实体 转为json 数据格式---jackson 的详细用法_Jackson快速入门