1,选择框可以让用户以滑动的方式选择值。示例如下:

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()
    }
}

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)的用法的更多相关文章

  1. 下拉选择框 Spinner的用法。

    代码如下: package com.lixu.xialakuang; import android.app.Activity; import android.content.Context; impo ...

  2. swift 实践- 12 -- UIPickerView

    import UIKit class ViewController: UIViewController , UIPickerViewDelegate,UIPickerViewDataSource{ v ...

  3. select2 选择框插件

    <select id="selBusi_type"><select> //初始化业务类型下拉 var initBusiTypeSel = function( ...

  4. Chosen:Select 选择框的华丽变身

    HTML Form 表单里的各种组件,例如文本输入框,textarea,按钮等,都可以通过CSS或其它技术进行美化,让它们看起来很漂亮了,唯独下拉列表选项框(select box),不管你怎么做,它摆 ...

  5. Java开发笔记(一百二十二)AWT选择框

    前面介绍了两种文本输入框的用法,不过实际应用很少需要用户亲自文字,而是在界面上列出几个选项,让用户勾勾点点完成选择,这样既方便也不容易弄错.依据选择的唯一性,可将选项控件分为两类:一类是在方框中打勾的 ...

  6. Java开发笔记(一百三十)Swing的选择框

    不管是AWT还是Swing,都把选择框分成两类:复选框和单选按钮,这两类控件无论是外观上还是功能上均有显著差异.例如,在外观方面,复选框是在方框内打勾,而单选按钮是在圆圈内画圆点:在功能方面,复选框允 ...

  7. css自定义 range radio select的样式滑轮,按钮,选择框

    写在前面: 之前踩坑css的时候,遇到滑轮,按钮,选择框这类型的东西,为了页面效果,总是需要自定义他们的样式,而不使用他们的默认样式.当时写的时候,我也是蛮头疼的,弄了个demo,链接在下面.对此做个 ...

  8. java、easyui-combotree树形下拉选择框

    最近一直在研究这个树形的下拉选择框,感觉非常的有用,现在整理下来供大家使用: 首先数据库的表架构设计和三级菜单联动的表结构是一样,(父子关系) 1.下面我们用hibernate建一下对应的额实体类: ...

  9. Notes: select选择框

    HTML选择框通过select标签创建,该元素是HTMLSelectElement的实例,拥有以下属性和方法: selectedIndex:选中项的索引 options:选择框的所有选项 add:向选 ...

随机推荐

  1. Datagridview列绑定数据

    属性最下面的Column项: 把每一列的字段绑定,更改显示的标题. 数据绑定代码: string sql = "select IncomeExpendTypeID , TypeName , ...

  2. c++空指针调用类成员函数

    最近在看C++动态绑定问题时(理解静态绑定时)发现的问题:能用空指针调用类的成员函数(gcc,vs2013下都可以). 例子: class animal { public: void sleep(){ ...

  3. 算法练习之DP 求LCM (最长公共子序列)

    1. 对于序列x[1,i]和y[1,j],推导递推公式1.a 假设当前元素同样,那么就将当前最大同样数+12.b 假设当前元素不同.那么就把当前最大同样数"传递"下去 因此递推公式 ...

  4. C# -- 什么是方法签名?

    签名指的是返回值和参数. 比如 : public void A ( int p1,int p2){} public void B ( int q1,int q2){} 的签名相同. 而 public ...

  5. WCF技术剖析之二十一: WCF基本的异常处理模式[上篇]

    原文:WCF技术剖析之二十一: WCF基本的异常处理模式[上篇] 由于WCF采用.NET托管语言(C#和NET)作为其主要的编程语言,注定以了基于WCF的编程方式不可能很复杂.同时,WCF设计的一个目 ...

  6. 基于visual Studio2013解决面试题之1001去除数字

     题目

  7. 基于visual Studio2013解决C语言竞赛题之1033数字交换

          题目 解决代码及点评 /* 功能:将一个一维数组中的偶数依次交换.例如有8个元素, 若其中第1.4.5三元素是偶数时应按下图交换. 例子: a[]={2,3,1,6 ...

  8. crm查询记录共享给了哪些人

    有时候,我们须要查询一个记录.共享给了哪些人?怎么做? 第一种做法:是sql的方式 select * from PrincipalObjectAccess where objectid = '5226 ...

  9. Codeforces 56D Changing a String 编辑距离 记忆dp

    主题链接:点击打开链接 编辑距离.,== 一边dp虽然录制前体累,,依然是dp #include<iostream> #include<cstdio> #include< ...

  10. Qt的setMouseTracking使用

    bool mouseTracking 这个属性保存的是窗口部件跟踪鼠标是否生效. 如果鼠标跟踪失效(默认),当鼠标被移动的时候只有在至少一个鼠标按键被按下时,这个窗口部件才会接收鼠标移动事件. 如果鼠 ...