iOS - UIPickerView
前言
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的更多相关文章
- iOS UIPickerView 显示全国省市
效果图 #import "ViewController.h" @interface ViewController () @property(strong,nonatomic)UIP ...
- ios UIPickerView 技巧集锦(包括循环滚动)
摘自: http://blog.csdn.net/ipromiseu/article/details/7436521 http://www.cnblogs.com/dabaopku/archive/2 ...
- iOS:UIPickerView选择器的使用
通过UIPickerView选择器做的一个类似于密码锁的日期时间表 源码如下: #import <UIKit/UIKit.h> @interface ViewController : UI ...
- iOS学习——UIPickerView的实现年月选择器
最近项目上需要用到一个选择器,选择器中的内容只有年和月,而在iOS系统自带的日期选择器UIDatePicker中却只有四个选项如下,分别是时间(时分秒).日期(年月日).日期+时间(年月日时分)以及倒 ...
- iOS学习之自定义弹出UIPickerView或UIDatePicker(动画效果)
前面iOS学习之UIPickerView控件的简单使用 用到的UIPickerView弹出来是通过 textField.inputView = selectPicker; textField.in ...
- iOS学习之UIPickerView控件的关联选择
接上篇iOS学习之UIPickerView控件的简单使用 接着上篇的代码 http://download.csdn.net/detail/totogo2010/4391870 ,我们要实现的效果如下: ...
- iOS中UIPickerView常见属性和方法的总结
UIPickerView是iOS中的原生选择器控件,使用方便,用法简单,效果漂亮. @property(nonatomic,assign) id<UIPickerViewDataSource&g ...
- [ios]新手笔记-。-UIPickerView 关于伪造循环效果和延时滚动效果
查找了网上资料,循环效果绝大部分都是增加行数来制造循环的错觉,延时滚动就是利用NSTimer间隔出发滚动事件来制造滚动效果. 代码: #import <UIKit/UIKit.h>#imp ...
- iOS:选择器控件UIPickerView的详解和演示
选择器控件UIPickerView: 功能:它能够创建一个类似于密码锁式的单列或多列的选择菜单,用户可以通过它设置的代理来选择需要菜单中的任意的数据.例如创建日历.字体表(类型.大小.颜色).图库等. ...
随机推荐
- inupt textarea提示文字(点击消失,不输入恢复)及限制字数
效果: input: textarea: 限100字 源码: input: <input name="textfield" type="text" max ...
- iOS抓包Charles 操作
今天就来看一下Mac上如何进行抓包,之前有一篇文章介绍了使用Fidder进行抓包 http://blog.csdn.net/jiangwei0910410003/article/details/198 ...
- aspnet excel导入导出SQLserver
http://my.csdn.net/libin690145955/code/detail/452 http://blog.csdn.net/ltoper/article/details/532980 ...
- python中时间的基本使用
格式化日期 我们可以使用 time 模块的 strftime 方法来格式化日期,: time.strftime(format[, t]) #!/usr/bin/python # -*- coding: ...
- js正则匹配
var account = $('input[name="account"').val(); var re = /^[0-9]+.?[0-9]*$/; if (!re.test(a ...
- JavaScript DOM 编程艺术(第2版)读书笔记(2)
JavaScript 语法 注释 单行注释:// 多行注释:/* */ "<!--"可以用作单行注释,由于和HTML的"<!-- -->"多 ...
- SDUT 2623:The number of steps
The number of steps Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 Mary stands in a stra ...
- 类型解释器——C专家编程读书笔记
对于声明,应该按下面的步骤来进行解释: 1) 声明从它的名字开始读取,然后按照优先级顺序依次读取 2) 优先级顺序 a) 括号括起来的部分 b) 后缀操作符,()表示函数,[]表示数组 c) 前缀操作 ...
- poj3667 Hotel
此题不难却易出错,很能考察思维的严谨性. 指定ll为区间内左端顶格数的连续可利用房间,rr为右端顶格数的数值,mm为区间内最长的连续可利用房间数. 在查询的时候,由于要返回最靠左的区间左端点,使得在该 ...
- wpfのpack协议
当引用的资源需要做成dll时,要用此协议 协议:pack:// 授权:有两种.一种用于访问编译时已经知道的文件,用application:///.一种用于访问编译时不知道.运行时才知道的文件 ...