效果显示数下图:

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. C#播放器控件的常用方法介绍

    右击工具箱->选择项(I)... -> 显示"选择工具箱项" -> COM组件 -> Windows Media Player   wmp.dll 添加 [ ...

  2. R ggplot2 线性回归

    摘自  http://f.dataguru.cn/thread-278300-1-1.html library(ggplot2) x=1:10y=rnorm(10)a=data.frame(x= x, ...

  3. cocos2dx遇到的坑1

    记录下在cocos2dx 2.x时代遇到的问题 1.节点的观念,用节点来管理 2.pushscene popscene 和replacewithscene runwithscene对应 3.lua里释 ...

  4. Swif基础语法01

    import Foundation /** *  1,第一个swift程序 */ println("Hello, World!") /** *  2,定义常量 */ let cIn ...

  5. jackson2.1.4 序列化 通过给定Class映射 与抽象类的映射

    //如果已知想要序列化的类型 可以使用TypeReference来进行处理 //List<MyBean> result = mapper.readValue(src, new TypeRe ...

  6. Android——滚动视图(ScrollView)图片视图(ImageView)、状态开关按钮(ToggleButton)、时钟

    xml <?xml version="1.0" encoding="utf-8"?> <!--滚动视图--> <ScrollVie ...

  7. swconfig--交换接口配置命令

    swconfig是交换接口(switch)配置命令. swconfig list ; 列出可用的SWITCH设备信息(dev参数) Found: switch0 - ag71xx-mdio. swco ...

  8. Spark MLlib回归算法------线性回归、逻辑回归、SVM和ALS

    Spark MLlib回归算法------线性回归.逻辑回归.SVM和ALS 1.线性回归: (1)模型的建立: 回归正则化方法(Lasso,Ridge和ElasticNet)在高维和数据集变量之间多 ...

  9. Apache2.4.x版wampserver本地php服务器如何让外网访问及启用.htaccess

    http://www.jb51.net/article/61193.htm ———————————————————————————————————————————— 这篇文章主要介绍了Apache2. ...

  10. Javascript函数声明与函数表达式的区别

    在定义函数时,我们一般使用下面这两种方法: 使用函数声明定义: 1 2 3 function  sum (a, b) {     return a + b; } 使用函数表达式定义: 1 2 3 va ...