前言

	NS_CLASS_AVAILABLE_IOS(2_0) __TVOS_PROHIBITED @interface UIPickerView : UIView <NSCoding, UITableViewDataSource>
@available(iOS 2.0, *) public class UIPickerView : UIView, NSCoding, UITableViewDataSource
  • 选择框可以让用户以滑动的方式选择值。

1、UIPickerView 的创建

  • 遵守协议 UIPickerViewDataSource, UIPickerViewDelegate

  • Objective-C

    	// 实例化 UIPickerView 对象
    UIPickerView *pickerView = [[UIPickerView alloc] init]; // 设置代理
    pickerView.dataSource = self;
    pickerView.delegate = self; // 将 pickerView 添加到屏幕
    [self.view addSubview:pickerView]; // 设置列数,必须设置,UIPickerViewDataSource 协议方法
    - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView { return 3;
    } // 设置行数 ,必须设置,UIPickerViewDataSource 协议方法
    - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { return 10;
    } // 设置各行内容,必须设置,UIPickerViewDelegate 方法
    - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { return [NSString stringWithFormat:@"%li行 - %li列", row, component];
    }
  • Swift

    	// 实例化 UIPickerView 对象
    let pickerView:UIPickerView = UIPickerView() // 设置代理
    pickerView.dataSource = self
    pickerView.delegate = self // 将 pickerView 添加到屏幕
    self.view.addSubview(pickerView) // 设置列数,必须设置,UIPickerViewDataSource 协议方法
    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int { return 3
    } // 设置行数,必须设置,UIPickerViewDataSource 协议方法
    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { return 10
    } // 设置各行内容,必须设置,UIPickerViewDelegate 方法
    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? { return String(format: "%li行 - %li列", row, component)
    }

2、UIPickerView 的设置

  • Objective-C

    	// 设置默认值
    [pickerView selectRow:1 inComponent:0 animated:YES]; // 第 0 列的默认值为 1
    [pickerView selectRow:2 inComponent:1 animated:YES]; // 第 1 列的默认值为 2
    [pickerView selectRow:3 inComponent:2 animated:YES]; // 第 2 列的默认值为 3 // 设置 frame
    /*
    高度只有三个值:162, 180 和 216,默认为 216,设置为其它的值无效
    */
    pickerView.frame = CGRectMake(10, 30, self.view.bounds.size.width - 20, 162); // 设置位置
    pickerView.center = self.view.center; // 设置背景颜色
    pickerView.backgroundColor = [UIColor orangeColor]; // 是否显示指示器
    /*
    default is NO
    */
    pickerView.showsSelectionIndicator = YES; // 刷新指定的列
    [pickerView reloadComponent:0]; // 刷新所有的列
    [pickerView reloadAllComponents]; // 获取列数,只读
    NSInteger numberOfComponents = pickerView.numberOfComponents; // 获取指定列的行数
    NSInteger numberOfRows = [pickerView numberOfRowsInComponent:0]; // 获取指定行的尺寸
    CGSize rowSize = [pickerView rowSizeForComponent:0]; // 获取指定列被选中的行数索引
    NSInteger selectedIndex = [pickerView selectedRowInComponent:0]; // 获取指定行列的视图
    UIView *view = [pickerView viewForRow:3 forComponent:0]; // 设置列宽
    /*
    不设置时为默认宽度,UIPickerViewDelegate 方法
    */
    - (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component { return 50;
    } // 设置行高
    /*
    不设置时为默认高度 32,UIPickerViewDelegate 方法
    */
    - (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component { return 50;
    }
  • Swift

    	// 设置默认值
    pickerView.selectRow(1, inComponent: 0, animated: true) // 第 0 列的默认值为 1
    pickerView.selectRow(2, inComponent: 1, animated: true) // 第 1 列的默认值为 2
    pickerView.selectRow(3, inComponent: 2, animated: true) // 第 2 列的默认值为 3 // 设置 frame
    /*
    高度只有三个值:162, 180 和 216,默认为 216,设置为其它的值无效
    */
    pickerView.frame = CGRectMake(10, 30, self.view.bounds.size.width - 20, 162) // 设置位置
    pickerView.center = self.view.center // 设置背景颜色
    pickerView.backgroundColor = UIColor.orangeColor() // 是否显示指示器
    /*
    default is NO
    */
    pickerView.showsSelectionIndicator = true // 刷新指定的列
    pickerView.reloadComponent(0) // 刷新所有的列
    pickerView.reloadAllComponents() // 获取列数,只读
    let numberOfComponents:Int = pickerView.numberOfComponents // 获取指定列的行数
    let numberOfRows:Int = pickerView.numberOfRowsInComponent(0) // 获取指定行的尺寸
    let rowSize:CGSize = pickerView.rowSizeForComponent(0) // 获取指定列被选中的行数索引
    let selectedIndex:Int = pickerView.selectedRowInComponent(0) // 获取指定行列的视图
    let view:UIView? = pickerView.viewForRow(3, forComponent: 0) // 设置列宽
    /*
    不设置时为默认宽度,UIPickerViewDelegate 方法
    */
    func pickerView(pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat { return 50
    } // 设置行高
    /*
    不设置时为默认高度 32,UIPickerViewDelegate 方法
    */
    func pickerView(pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat { return 50
    }

3、UIPickerViewDataSource 协议方法

  • Objective-C

    	// 设置列数
    - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView { } // 设置行数
    - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { }
  • Swift

    	// 设置列数
    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int { } // 设置行数
    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { }

4、UIPickerViewDelegate 协议方法

  • Objective-C

    	// 设置各行内容为 字符串
    - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { } // 设置各行内容为 NSAttributedString 型字符串
    - (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component { } // 设置各行内容为 view
    - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view { } // 设置列宽
    - (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component { } // 设置行高
    - (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component { } // 检测行的选择状态,在滑动停止后触发
    - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { }
  • Swift

    	// 设置各行内容为 字符串
    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? { } // 设置各行内容为 NSAttributedString 型字符串
    func pickerView(pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? { } // 设置各行内容为 view
    func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView?) -> UIView { } // 设置列宽
    func pickerView(pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat { } // 设置行高
    func pickerView(pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat { } // 检测行的选择状态,在滑动停止后触发
    func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { }

iOS - UIPickerView的更多相关文章

  1. iOS UIPickerView 显示全国省市

    效果图 #import "ViewController.h" @interface ViewController () @property(strong,nonatomic)UIP ...

  2. ios UIPickerView 技巧集锦(包括循环滚动)

    摘自: http://blog.csdn.net/ipromiseu/article/details/7436521 http://www.cnblogs.com/dabaopku/archive/2 ...

  3. iOS:UIPickerView选择器的使用

    通过UIPickerView选择器做的一个类似于密码锁的日期时间表 源码如下: #import <UIKit/UIKit.h> @interface ViewController : UI ...

  4. iOS学习——UIPickerView的实现年月选择器

    最近项目上需要用到一个选择器,选择器中的内容只有年和月,而在iOS系统自带的日期选择器UIDatePicker中却只有四个选项如下,分别是时间(时分秒).日期(年月日).日期+时间(年月日时分)以及倒 ...

  5. iOS学习之自定义弹出UIPickerView或UIDatePicker(动画效果)

    前面iOS学习之UIPickerView控件的简单使用 用到的UIPickerView弹出来是通过 textField.inputView = selectPicker;   textField.in ...

  6. iOS学习之UIPickerView控件的关联选择

    接上篇iOS学习之UIPickerView控件的简单使用 接着上篇的代码 http://download.csdn.net/detail/totogo2010/4391870 ,我们要实现的效果如下: ...

  7. iOS中UIPickerView常见属性和方法的总结

    UIPickerView是iOS中的原生选择器控件,使用方便,用法简单,效果漂亮. @property(nonatomic,assign) id<UIPickerViewDataSource&g ...

  8. [ios]新手笔记-。-UIPickerView 关于伪造循环效果和延时滚动效果

    查找了网上资料,循环效果绝大部分都是增加行数来制造循环的错觉,延时滚动就是利用NSTimer间隔出发滚动事件来制造滚动效果. 代码: #import <UIKit/UIKit.h>#imp ...

  9. iOS:选择器控件UIPickerView的详解和演示

    选择器控件UIPickerView: 功能:它能够创建一个类似于密码锁式的单列或多列的选择菜单,用户可以通过它设置的代理来选择需要菜单中的任意的数据.例如创建日历.字体表(类型.大小.颜色).图库等. ...

随机推荐

  1. 启动hadoop报192.168.1.151: Address 192.168.1.151 maps to node1, but this does not map back to the address - POSSIBLE BREAK-IN ATTEMPT!

    使用root用户启动hadoop的时候报错: [root@node1 ~]# su - hadoop -c start-all.sh starting namenode, logging to /ap ...

  2. Browser对象

    Window对象即浏览器中打开的窗口,当文档里面有框架(frame或者iframe标签)时,浏览器会为HTML文档创建一个window对象,并为每个框架创建一个额外的window对象. 属性close ...

  3. 周赛-Expression 分类: 比赛 2015-08-02 09:35 3人阅读 评论(0) 收藏

    A. Expression time limit per test1 second memory limit per test256 megabytes inputstandard input out ...

  4. 动态规划(DP),模拟

    题目链接:http://poj.org/problem?id=1088 Memory: 252KTime: 16MSLanguage: C++Result: Accepted 解题报告: 1.lm[i ...

  5. HDU 4910 Problem about GCD 找规律+大素数判断+分解因子

    Problem about GCD Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  6. AIM Tech Round 3 (Div. 2) B

    Description Vasya takes part in the orienteering competition. There are n checkpoints located along ...

  7. HTTP缓存机制

    缓存对于移动端是非常重要的存在. 减少请求次数,减小服务器压力. 本地数据读取速度更快,让页面不会空白几百毫秒. 在无网络的情况下提供数据. 缓存一般由服务器控制(通过某些方式可以本地控制缓存,比如向 ...

  8. vim基本使用

    i 进入插入状态 esc 退出插入状态 x 删除一个字符 dd 删除一行,并拷贝 yy 拷贝 p 粘贴 u 撤销 ctrl+r 重做 :w 保存 :q 退出 :q! → 退出不保存

  9. 5-JS函数

    函数 定义函数 JS中有3种定义函数的方法: 函数声明 用函数声明定义函数的方式如下: function abs(x) { if (x >= 0) { return x; } else { re ...

  10. U盘安装CentOS7的最终解决方案

    转载自http://www.augsky.com/599.html 终于将CentOS7装上笔记本了,过程无比艰辛,因为我发现网上大家提到的所有U盘安装CentOS7时碰到的问题几乎都被我碰到了,像什 ...