新风作浪博客学习(八)代码实现UIPickerView .
先说一下当个组件选取器,我们创建一个数组NSAray来保存选取器中的内容;选取器本身不会储存任何数据,,它通过调用数据源和委托方法来显示数据;但是对于大量数据的数据源,数组并不合适,我们可以做一个静态列表如plist文件或者URL载入,和后面将讲在文件中获取数据,还以多个选取器的之间的关联如何实现;先说下简单的单个选取器:
先把效果图贴出来
[img]
[img]http://dl.iteye.com/upload/attachment/0079/5116/c682c3a8-57e5-3577-8a69-116e21c75015.png[/img]
[/img]
[img]
[img]http://dl.iteye.com/upload/attachment/0079/5118/8d2a5250-6364-37ab-87a4-e53bd83af8fa.png[/img]
[/img]
1.新建工程名为PickerViewDemo , File->New->Project ->single View Application -> next
[img]
[img]http://dl.iteye.com/upload/attachment/0079/5120/881862d1-32b7-3918-81e5-6d2c3e1be217.png[/img]
[/img]
2.在视图上添加选取器
pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, 320, 216)];// 指定Delegate pickerView.delegate=self;// 显示选中框 pickerView.showsSelectionIndicator=YES; [self.view addSubview:pickerView];
以上可以在视图显示一个选取器,但是内容空白,pickerView.showsSelectionIndicator=YES;是这只当前选取器所选中的内容:
选取器上显示数据,必须依赖两个协议,UIPickerViewDelegate和UIPickerViewDataSource,把他们添加到ViewController.h文件中
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UIPickerViewDelegate,UIPickerViewDataSource>{
UIPickerView *pickerView; NSArray *pickerData;
}
@end
3.然后在.m文件的ViewDidLoad中初始化界面
[img]- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, 320, 216)];
// 指定Delegate
pickerView.delegate=self;
// 显示选中框
pickerView.showsSelectionIndicator=YES;
[self.view addSubview:pickerView];
NSArray *dataArray = [[NSArray alloc]initWithObjects:@"许嵩",@"周杰伦",@"梁静茹",@"许飞",@"凤凰传奇",@"阿杜",@"方大同",@"林俊杰",@"胡夏",@"邱永传", nil];
pickerData=dataArray;
// 添加按钮
CGRect frame = CGRectMake(120, 250, 80, 40);
UIButton *selectButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
selectButton.frame=frame;
[selectButton setTitle:@"SELECT" forState:UIControlStateNormal];
[selectButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:selectButton];
}[/img]
4.实现UIPickerView的代理方法,将数据显示在选取器上所需要几个方法
#pragma mark -#pragma mark Picker Date Source Methods
//返回显示的列数-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{ return 1;}//返回当前列显示的行数-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{ return [pickerData count];}
#pragma mark Picker Delegate Methods
//返回当前行的内容,此处是将数组中数值添加到滚动的那个显示栏上-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{ return [pickerData objectAtIndex:row];}
前两个是数据源的代理方法,一个是返回列,有几个选取器就返回几,第二个是设置选取器有多少行,因为就这一个选取器,所以直接返回行数,即数组元素个数多少;第三个代理方法是将数组元素添加到了选取器上面显示;
UIPickerViewDelegate中的实例方法
-(void) pickerView: (UIPickerView *)pickerView didSelectRow: (NSInteger)row inComponent: (NSInteger)component
作用: 当用户选择某个row时,picker view调用此函数
参数: pickerView representing the picker view request the data
-(CGFloat) pickerView:(UIPickerView *)pickerView rowHeightForComponent: (NSInteger) component
作用:由picker view调用,当其在绘制row内容,需要row的高度时
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger) component
作用: 当picker view需要给指定的component.row指定title时,调用此函数
-(UIView *)pickerView: (UIPickerView *)pickerView view ForRow:(NSInteger) row forComponent:(NSInteger) component reusingView:(UIView *) view
作用: 当picker view需要给指定的component.row指定view时,调用此函数.返回值为用作row内容的view
参数: view参数, a view object that was previously used for this rows, but is now hidden and cached by the picker view
- (CGFloat)pickerView: (UIPickerView *)pickerView widthForComponent:(NSInteger) component
作用:当picker view 需要row的宽度时,调用此函数
UIPickerViewDataSource中的实例方法:
按照官方文档的说法,UIPickerViewDataSource这个协议仅有的功能就是提供picker view中component的个数和各个component中的row的个数,虽然名为datasource,但是它工作于MVC的C中
本协议仅有两个实例方法,均需要实现
-(NSInteger) numberOfComponentsInPickerView:(UIPickerView *)pickerView
作用:返回pickerView应该有几个component
-(NSInteger) pickerView:(UIPickerView *) pickerView numberOfRowsInComponent:(NSInteger) component
作用:返回指定component应该有几个row
5.关于按钮响应事件,关于按钮的形成和添加响应事件不再提,前面都有,
-(void) buttonPressed:(id)sender{ NSInteger row =[pickerView selectedRowInComponent:0]; NSString *selected = [pickerData objectAtIndex:row]; NSString *message = [[NSString alloc] initWithFormat:@"你选择的是:%@",selected];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil]; [alert show];
}
[pickerViewselectedRowInComponent:0];方法返回当前被选中的索引序号,这是UIPickerView的实例方法,在官方文档中
UIPickerView还有其他实例方法
- (NSInteger) numberOfRowsInComponent:(NSInteger)component
参数为component的序号(从左到右,以0起始),返回指定的component中row的个数
-(void) reloadAllComponents
调用此方法使得PickerView向delegate: Query for new data for all components
-(void) reloadComponent: (NSInteger) component
参数为需更新的component的序号,调用此方法使得PickerView向其delegate: Query for new data
-(CGSize) rowSizeForComponent: (NSInteger) component
参数为component的序号,返回值为the size of rows in the given components, picker view 通过调用委托方法中的pickerView:widthForComponent:和pickerView:rowHeightForComponent:获得返回值
-(NSInteger) selectedRowInComponent: (NSInteger) component
参数为component的序号,返回被选中row的序号,若无row被选中,则返回-1
-(void) selectRow: (NSInteger)row inComponent: (NSInteger)component animated: (BOOL)animated
作用:在代码指定要选择的某component的某row
参数:row序号,component序号,BOOL值(若为YES,转动spin到你选择的新值,若为NO,直接显示你选择的值)
-(UIView *) viewForRow: (NSInteger)row forComponent: (NSInteger)component
参数:row序号,component序号,返回由委托方法pickerView:viewForRow:forComponentreusingView:指定的view.如果委托对象并没有实现这个方法,或者说这个view并不是可见的,则返回nil
新风作浪博客学习(八)代码实现UIPickerView .相关推荐
- 实时推荐服务建设——基于Spark平台的协同过滤实时电影推荐系统项目系列博客(八)
系列文章目录 初识推荐系统--基于Spark平台的协同过滤实时电影推荐系统项目系列博客(一) 利用用户行为数据--基于Spark平台的协同过滤实时电影推荐系统项目系列博客(二) 项目主要效果展示--基 ...
- 【机器学习】<刘建平Pinard老师博客学习记录>Scikit-learnPandas(NumpyMatplotlib)学习线性回归
目录 一.数据获取&问题定义: 1.数据获取: 2.问题定义: 二.整理数据: 三.使用Pandas读取数据: 1.导入依赖库: 2.使用Pandas读取函数: 四.数据准备: 1.查看数据: ...
- php博客模板源码下载,Z-BlogPHP博客学习SEM网站模板 v1.3
Z-BlogPHP博客学习SEM网站模板是一个专门为Z-BlogPHP博客系统进行设计的教程类博客网站主题模板. Z-BlogPHP博客学习SEM网站模板 v1.3更新日志 1.多说停止使用,改为zb ...
- 第一篇博客-学习Markdown
第一篇博客-学习Markdown 学习Markdown 学习Markdown 井号+空格+标题:几个#就是几级标题 两星+文本+两星:加粗 一星+文本+一星:斜体 三星+文本+三星:斜体加粗 两波浪+ ...
- 博客园写代码发家的大牛
博客园写代码发家的大牛 吉日嘎拉: http://www.cnblogs.com/jirigala/ 当年他所开发的 通用权限管理系统 魏琼东: http://www.cnblogs.com/east ...
- MARKET1501的学习,跟着苏同学的博客学习
MARKET1501的学习,跟着苏同学的博客学习 先看看官方文档:然后附上苏的博客链接http://bigbrothersue.com/index.php/2017/12/20/person-re-i ...
- 由oschina.neT了解到博客备份的代码
为什么80%的码农都做不了架构师?>>> 由oschina.neT了解到博客备份的代码 ========================= 通过红薯大哥的文章知道,用地址代码 ...
- 博客园 CSS 代码定制
@charset "utf-8"; body {font-size: 15px;padding: 0;margin: 0;font-family: "微软雅黑" ...
- 传智博客学习笔记8--面向对象下
传智博客学习笔记8--面向对象下 2009-3-12 15:41:42 继承 extends 接口 interface 如果一个抽象类中的所有方法都是抽象的,我们就可以将这个类用另外一种方式来定义,也 ...
最新文章
- 不得不知的小程序基本知识
- Java基础:JDK、JRE、JVM的区别与联系
- 从工作经历和实践理论看工业互联网的发展
- 2016 年 ACM/ICPC 青岛区域赛 Problem C Pocky
- uvm_reg_defines——寄存器模型(四)
- (转)C#开发微信门户及应用(5)--用户分组信息管理
- 日常邮件用语(一)网摘学习
- POJ 1008 玛雅日历
- 来自H3C的降维打击:H3C BX54鲸路由评测体验
- 有道云生成html,从有道云笔记迁移到为知笔记
- 每日必读DZone news - 2022年2月十大DZone文章
- git使用说明--新手安装必备
- 操作系统实验三:主存空间的分配与回收
- 【教程】EasyDSS演示模式播放ws-flv格式视频流,如何控制3分钟自动跳转登录页?
- [iOS开发]Status Bar Style
- TCP/IP篇(1)--协议模型
- 条码标签打印机打印头金线包封封装用胶方案
- 广州签发全国首张微信身份证,AI成主要证明技术
- 西门子S7-1200控制5轴伺服程序加维纶触摸屏画面案例
- 去耦电容和旁路电容的区别,终于有人说清楚了!
热门文章
- 一拳对着漫天的金色沙粒狠狠捣出
- 初识 PS CS6(五)___透视变换, 精确变换
- IDS(***检测)要“退休”了吗?
- 串口突然接收不到数据:ORE:过载错误 (Overrun error)
- “笨兔数独” 解数独软件 介绍及使用指南
- Power Automate 中的 SharePoint Trigger Conditions 配置
- 软件测试入门笔记(测试基础)by.肖洪福
- 数学建模模型10——种群增长
- Android Screen Monitor抓取真机屏幕
- 微型真空水泵WAJ280降低工作电压测试报告