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: 功能:它能够创建一个类似于密码锁式的单列或多列的选择菜单,用户可以通过它设置的代理来选择需要菜单中的任意的数据.例如创建日历.字体表(类型.大小.颜色).图库等. ...
随机推荐
- 理解css中的position-static\relative\fixed\absolute
position属性有四个值: static(静态定位):是默认值,不会被特殊的定位,遵循正常的文档流对象,对象占用文档空间,该方式下,top.right.bottom.left.z-index等属性 ...
- 求两个数的最大公约数(Java)
获得两个随机数(100以内),并放入数组中 public int[] getTwoRandom(){ int[] t = new int[2]; Random rand = new Random(); ...
- 【转】Firefox快捷键
转载地址: http://www.douban.com/note/140139119/ Ctrl + 数字键来打开第N个标签页这种还要先数完再到键盘上找数字Ctrl + Page Up = 激活左边一 ...
- 利用反射及jdbc元数据实现通用的查询方法
---------------------------------------------------------------------------------------------------- ...
- 关于BufferedWriter.write超过30W条数据写入过慢问题。
原创文章,转载请注明出处! ------------------------------------------------------------ 今天接到一个项目需求变更,是关于从数据库查询到30 ...
- 知乎上有一个问题“在mfc框架中,有上面方法能直接将opencv2.0库中的Mat格式图片传递到Picture Control”中显示?
一直以来,我使用的方法都是shiqiyu在opencvchina上面提供的引入directshow,并且采用cvvimage和cameraDs的方法.这个方法虽然在xp/win7/win8下面都能够成 ...
- await和async关键字来写异步程序
await和async关键字出现于.Net5.0,方便写异步程序. 例子: public class MyClass { public MyClass() { DisplayValue(); //这里 ...
- jQuery 一些小技巧
1. 返回顶部按钮 可以利用 animate 和 scrollTop 来实现返回顶部的动画,而不需要使用其他插件. // Back to top ...
- poj3349
http://poj.org/problem?id=3349 每个雪花都有六个分支,用六个整数代表,这六个整数是从任意一个分支开始,朝顺时针或逆时针方向遍历得到的.输入多个雪花,判断是否有形状一致的雪 ...
- VC++界面编程之--阴影窗口的实现详解
转载:http://blog.csdn.net/rmxming/article/details/11661365 对于我们这些控件狂来说,窗口阴影也是一个必不可少的实现需求.虽说其没多大用,但对于增加 ...