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: 功能:它能够创建一个类似于密码锁式的单列或多列的选择菜单,用户可以通过它设置的代理来选择需要菜单中的任意的数据.例如创建日历.字体表(类型.大小.颜色).图库等. ...
随机推荐
- 4、BOM编程/正则表达式
1. BOM编程 1.1. BOM编程基础 全称 Browser Object Model,浏览器对象模型. JavaScript是由浏览器中内置的javascript脚本解释器程序来执行jav ...
- SpringMVC项目,启动项目怎么总是报找不到log4j.properties文件
具体操作:右键项目---->properties--->Java Build Path--->source--->Add Folder --->选择log4.proper ...
- mysql之使用xtrabackup进行物理备份、恢复、在线克隆从库、在线重做主从
注:图片来自<深入浅出MySQL 数据库开发 优化与管理维护 第2版> 物理备份和恢复 1.冷备份:停掉mysql再备份,一般很少用,因为很多应用不允许长时间停机,停机备份的可以直接CP数 ...
- python 拷贝文件夹下所有的文件到指定文件夹(不包括目录)
1.随便简单些写了一下.直接粘结代码,只是简单的实现一下,还很多需要完善和扩展的地方,比如忽略掉后缀文件,删除文件 如果排除的某些的话可以用: sourceF.find('.后缀')>0 2.注 ...
- String框架搭建的基本步骤,及从 IOC & DI 容器中获取 Bean(spring框架bean的配置)--有实现数据库连接池的链接
Spring框架的插件springsource-tool-suite-3.4.0.RELEASE-e4.3.1-updatesite(是一个压缩包)导入步骤: eclipse->help-> ...
- JAVA基础知识之Set集合
Set集合的基本特征是不记录添加顺序,不允许元素重复(想想是为什么).最常用的实现类是HashSet. 本文将要介绍以下内容 HashSet类 HashSe的特征 HashSet的equals和has ...
- Uva 10562 看图写树
题目链接:https://uva.onlinejudge.org/external/105/10562.pdf 紫书P170 直接在二维数组上做DFS,用的fgets函数读入数据,比较gets函数安全 ...
- Python网络爬虫Scrapy框架研究 以及 代理设置
地址:https://github.com/yidao620c/core-scrapy 例子:https://github.com/geekan/scrapy-examples 中文翻译文档: htt ...
- sys模块的初步认识
#!/usr/bin/python # Filename: cat.py import sys def readfile(filename): '''Print a file to the stand ...
- 项目文件中含有两个config文件,app.config与app1.config,如何获取app1.config中的配置
想要通过配置文件配置C#前台画面,好奇做了以下测试:在项目中新建了app.config与app1.config两个配置文件,请教一下各位高手如果想从app1.config中读取配置信息应该如何读取?采 ...