Swift - 选择框(UIPickerView)的用法
|
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 UIKitclass 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() }} |
2,调整选择框的尺寸
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} |
3,将图片作为选择框选项
选择框选项的内容,除了可以使字符串类型的,还可以是任意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} |
4,检测响应选项的选择状态
|
1
2
3
4
5
|
func pickerView(pickerView: UIPickerView!,didSelectRow row: Int, inComponent component: Int){ //将在滑动停止后触发,并打印出选中列和行索引 println(component) println(row)} |
Swift - 选择框(UIPickerView)的用法的更多相关文章
- 下拉选择框 Spinner的用法。
代码如下: package com.lixu.xialakuang; import android.app.Activity; import android.content.Context; impo ...
- swift 实践- 12 -- UIPickerView
import UIKit class ViewController: UIViewController , UIPickerViewDelegate,UIPickerViewDataSource{ v ...
- select2 选择框插件
<select id="selBusi_type"><select> //初始化业务类型下拉 var initBusiTypeSel = function( ...
- Chosen:Select 选择框的华丽变身
HTML Form 表单里的各种组件,例如文本输入框,textarea,按钮等,都可以通过CSS或其它技术进行美化,让它们看起来很漂亮了,唯独下拉列表选项框(select box),不管你怎么做,它摆 ...
- Java开发笔记(一百二十二)AWT选择框
前面介绍了两种文本输入框的用法,不过实际应用很少需要用户亲自文字,而是在界面上列出几个选项,让用户勾勾点点完成选择,这样既方便也不容易弄错.依据选择的唯一性,可将选项控件分为两类:一类是在方框中打勾的 ...
- Java开发笔记(一百三十)Swing的选择框
不管是AWT还是Swing,都把选择框分成两类:复选框和单选按钮,这两类控件无论是外观上还是功能上均有显著差异.例如,在外观方面,复选框是在方框内打勾,而单选按钮是在圆圈内画圆点:在功能方面,复选框允 ...
- css自定义 range radio select的样式滑轮,按钮,选择框
写在前面: 之前踩坑css的时候,遇到滑轮,按钮,选择框这类型的东西,为了页面效果,总是需要自定义他们的样式,而不使用他们的默认样式.当时写的时候,我也是蛮头疼的,弄了个demo,链接在下面.对此做个 ...
- java、easyui-combotree树形下拉选择框
最近一直在研究这个树形的下拉选择框,感觉非常的有用,现在整理下来供大家使用: 首先数据库的表架构设计和三级菜单联动的表结构是一样,(父子关系) 1.下面我们用hibernate建一下对应的额实体类: ...
- Notes: select选择框
HTML选择框通过select标签创建,该元素是HTMLSelectElement的实例,拥有以下属性和方法: selectedIndex:选中项的索引 options:选择框的所有选项 add:向选 ...
随机推荐
- 跨平台运行ASP.NET Core 1.0(转载)
前言 首先提一下微软更名后的叫法: ASP.NET 5 更名为 ASP.NET Core 1.0 .NET Core 更名为 .NET Core 1.0 Entity Framework 7 更名为 ...
- servlet 将输入内容通过拼接页面的方式显示出来
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...
- 17.1 Replication Configuration 复制:
17.1 Replication Configuration 复制: 17.1.1 How to Set Up Replication 17.1.2 Replication Formats 17.1. ...
- 基于visual Studio2013解决C语言竞赛题之0611素数排序
题目
- The Apache™ Batik Project
Apache(tm) Batik SVG Toolkit - a Java-based toolkit for applications or applets that want to use ima ...
- uva 10313 Pay the Price(完全背包)
题目连接:10313 - Pay the Price 题目大意:有0~300这300种价值的金额. 现在可能给出参数: 1个:n, 输出可以组成价值n的方式的个数. 2个:n, a输出用个数小于a的价 ...
- PCI-X总线
PCI-X接口是并连的PCI总线(Peripheral Components Interconnect)的更新版本号,仍採用传统的总线技术,只是有很多其它数量的接线针脚, 同一时候,如前所述的全部的连 ...
- MySQL推出Applier,可实时复制数据到Hadoop
MySQL复制操作可以将数据从一个MySQL服务器(主)复制到其他的一个或多个MySQL服务器(从).试想一下,如果从服务器不再局限为一个MySQL服务器,而是其他任何数据库服务器或平台,并且复制事件 ...
- 基于visual Studio2013解决面试题之1402选择排序
题目
- Android漫游记(4)---.so文件动态调试一例
Android平台的动态调试一直以来是个困扰我等Coder的头疼问题,特别是对于本地的动态调试支持.能够说是"弱智"级别的,不知道Google的新版NDK和新出的Android S ...