效果显示数下图:

1、初始化

pickerView.center = self.view.center
//将dataSource设置成自己
pickerView.dataSource=self
//将delegate设置成自己
pickerView.delegate=self
//设置选择框的默认值
pickerView.selectRow(,inComponent:,animated:true)
pickerView.selectRow(,inComponent:,animated:true)
pickerView.selectRow(,inComponent:,animated:true)
self.view.addSubview(pickerView)

2、代理方法的实现

func numberOfComponents(in pickerView: UIPickerView) -> Int {
return
} func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return
}
//设置选择框个选项的内容
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return String(row)+"_"+String(component)
    }

3、触摸按钮时,获得被选中的索引

   func btnClick() {

        let strs = String(pickerView.selectedRowInComponent()) + "_" + String(pickerView.selectedRowInComponent()) + "_" + String(pickerView.selectedRowInComponent())

         let alertControllers = UIAlertController(title: "系统提示",message: strs,preferredStyle: .alert)
let cancelActions = UIAlertAction(title: "取消",style: .cancel,handler: nil)
let okActions = UIAlertAction(title: "确定",style: .default,handler: nil)
alertControllers.addAction(cancelActions)
alertControllers.addAction(okActions) self.present(alertControllers, animated: true, completion: nil)
}

4、调整选择框的尺寸

func pickerView(pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat {
if == component
{
//第一列变宽
return
}else
{
//第二、三列变宽
return
}
}

5、设置行高

 func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
        return 40
    }

6、检测响应选项的选择状态

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
//将在滑动停止后触发,并打印出选中列和行索引
print(component)
print(row)
}

7,将图片作为选择框选项

    //将图片作为选择框选项
//选择框选择的内容,除了可以是字符串类型的,还可以是任意UIView类型的元素,比如我们将选项内容设置为图片:
func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
        let  image = UIImage(named:"11")
        let himageView = UIImageView()
        himageView.frame = CGRect(x:5,y:0,width:30,height:30)
        himageView.image = image
        
        return himageView
    }

点击按钮,显示被选中的行数,效果图如下:

swift - UIPickerView 的使用的更多相关文章

  1. UIPickerView swift

    // // ViewController.swift // UILabelTest // // Created by mac on 15/6/23. // Copyright (c) 2015年 fa ...

  2. iOS开发——UI篇Swift篇&UIPickerView

    UIPickerView //返回按钮事件 @IBAction func backButtonClick() { self.navigationController?.popViewControlle ...

  3. Swift - 选择框(UIPickerView)的用法

    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 ...

  4. Swift基础之UIPickerView和小animate的使用

    写一个简单的UIPickerView的使用Demo,比较简单,其中和一个小动画的结合使用 UIPickerView的使用基本上跟OC语言中的一样,就是写法的样式问题,想必开发过OC的应该不需要多讲了, ...

  5. swift 实践- 12 -- UIPickerView

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

  6. Swift学习之熟悉控件

    最近是比较清闲一些的,对于一个开发者来说,这也是一个很好的充电机会.以前做项目都是使用Objective-C去开发,但我们都知道,Swift语言从2014年的出现到现在,一步一步变的完善,渐渐变的受欢 ...

  7. iOS - UIPickerView

    前言 NS_CLASS_AVAILABLE_IOS(2_0) __TVOS_PROHIBITED @interface UIPickerView : UIView <NSCoding, UITa ...

  8. iOS开发——UI篇Swift篇&玩转UItableView(一)基本使用

    UItableView基本使用 class ListViewController: UIViewController , UITableViewDataSource, UITableViewDeleg ...

  9. Swift学习之常用UI的使用

    Swift学习之常用UI的使用 最近笔者在开始学习苹果最新的编程语言,因为笔者认为,苹果既然出了这门语言就绝对不会放弃,除非苹果倒闭了(当然这里知识一个玩笑). 所以在不久的将来,swift绝对是iO ...

随机推荐

  1. Ubuntu12.04下apache服务器的安装也配置

    安装步骤 1.下载httpd http://httpd.apache.org/ 2.解压缩httpd tar vxzf httpd-2.4.6.tar.gz 3.安装httpd 进入解压缩后的目录 c ...

  2. LeetCode: Word Search 解题报告

    Word SearchGiven a 2D board and a word, find if the word exists in the grid. The word can be constru ...

  3. LeetCode: Sort Colors 解题报告

    Sort ColorsGiven an array with n objects colored red, white or blue, sort them so that objects of th ...

  4. VisualVM使用Jstatd和JMX远程监控配置(转载)

    一.Jstatd远程监控服务器配置 jstat可以实时显示本地或远程JVM进程中类装载.内存.垃圾收集.JIT编译等数据.如果要显示远程JVM信息,需要远程主机开启RMI支持,详情如下: 1. 在JA ...

  5. 几种任务调度的 Java 实现方法与比较 mark

    任务调度是指基于给定时间点,给定时间间隔或者给定执行次数自动执行任务.本文由浅入深介绍四种任务调度的 Java 实现: Timer ScheduledExecutor 开源工具包 Quartz 开源工 ...

  6. 【火狐FireFox】同步失败后,书签被覆盖,如何恢复书签

    问题场景: 使用公司的电脑,下载安装火狐,登录个人帐号后,火狐会自动开始同步书签.但有时候会同步失败,比如登录之前选的是[本地服务],而最新的书签都是在[全球服务]理,那么很有可能同步到的是N久之前的 ...

  7. 如何测试Linux 中的wait函数能不能等待子进程的子进程?

    #include <stdio.h> #include <stdlib.h> int main() { pid_t pid = fork(); switch(pid) { : ...

  8. openwrt使用list

    openwrt中用到双向无头链表,实际应用时应在外部定义实体链表头,后续可直接应用链表函数(宏定义已将链表头排除在外): static struct list_head timeouts = LIST ...

  9. Hive shell 命令

    Hive shell 命令. 连接 hive shell 直接输入 hive 1.显示表 hive> show tables; OK test Time taken: 0.17 seconds, ...

  10. CodeIgniter(3.1.4)框架中添加执行时间统计代码

    CodeIgniter(3.1.4)框架中添加,执行时间统计代码: system/core/CodeIgniter.php最后行处. /* * ---------------------------- ...