iOS—使用picker View

一、实现效果

说明:点击随机按钮,能够自动选取,下方数据自动刷新。

二、实现思路

1.picker view的有默认高度为162,不可修改。

2.显示数据,需要设置数据源,也有两种方式(成为数据源,遵守协议)

3.实现数据源里面的两个方法

1)返回一共有多少列

2)在这一列中一共有多少行

4.通过代理告诉它那一列的哪一行显示哪些数据(设置其代理为控制器)

5.使用懒加载,加载所有的食物

6.完成基本数据的展示(列,行,内容)

7.自动更新选中的食物信息。(使用一个大的view,上面放6个label)

1)给3个lab赋值,添加三个属性(水果,主菜,饮料)

2)监听选中了哪一行(监听有两种思想,一个是代理,一个是通知),先查看有没有代理的方法(didselectRow)这个方法当选中了某一行的的时候调用,会将选中的列号和行号当做参数传入进去。能够获取到对应的列号和行号。

3)完成选中时调用的监听方法

4)在viewdidload里面设置默认选中的内容,设置为[0][1]

5)提高可扩展性(手动的调用那几行-使用一个for循环)

8.随机功能的实现

1)怎么让代码选中某一行(selectrow),调用该方法可以指定让它滚动到那一列的哪一行

2)实现头部的功能(使用一个大的uiview,里面放两个子控件)

3)设置高度44,怎么让随机按钮的位置居中?可以设置它的高度为44,最大的Y值为64。

4)设置随机按钮的点击事件randomFood,让pickerview主动选中某一行。

5)生成随机数的方法(生成随机数的限制,不超过当前的总数)

6)缺点,将来数据改变之后,会报错(模于几)[self.foods[0] count]?为什么不用简写 点语法?(切记要记住)

7)随机数的处理不严谨,有的时候生成的随机数可能是相等的,那么这样的话列就不会滚动,获取到对应列的数据总数,如何拿到上一次产生的随机值(也就是当前选中的行),比较上一次的行号和当前生成的随机数是否相同,如果相同则重写生成

9.解决另外一个问题,下面的数据随机刷新失效了,通过代码选中某一行。

三、实现代码示例

1.项目文档结构和storyboard文件

storyboard文件大的界面设置:

2.代码示例

主控制器文件代码:

1 //

2 // YYViewController.m

3 // 06-简单选菜系统的实现

4 //

​ 5 // Created by apple on 14-6-5. 6 // Copyright (c) 2014年 itcase. All rights reserved.

​ 7 //

8

​ 9 #import "YYViewController.h"

10

​ 11 //遵守数据源和代理协议

12 @interface YYViewController () 13 16 @property (strong, nonatomic) IBOutlet UILabel *fruitLab; 17 20 @property (strong, nonatomic) IBOutlet UILabel *stapleLab; 21 24 @property (strong, nonatomic) IBOutlet UILabel *drinkLab; 25 28 @property(nonatomic,strong)NSArray *foods; 29 @property (weak, nonatomic) IBOutlet UIPickerView *pickerView; 30 - (IBAction)randomFood:(id)sender; 31 32 @end 33 34 @implementation YYViewController 35 36- (void)viewDidLoad 37 { 38 [super viewDidLoad]; 39 40 //在这里设置下方数据刷新部分的初始显示 41 for (int component = 0; component) { 42 [self pickerView:nil didSelectRow:0inComponent:component]; 43 } 44 } 45 46 #pragma mark-使用懒加载,把数据信息加载进来47 -(NSArray *)foods 48 { 49 if (_foods==nil) { 50 NSString *fullpath=[[NSBundle mainBundle]pathForResource:@"foods.plist" ofType:nil]; 51 NSArray *arrayM=[NSArray arrayWithContentsOfFile:fullpath]; 52 _foods=arrayM; 53 } 54 return _foods; 55 } 56 57#pragma mark-处理随机按钮的点击事件 58 - (IBAction)randomFood:(id)sender { 59 60 // 让pickerView主动选中某一行 61 // 让pickerView选中inComponent列的Row行 62 //[self.pickerView selectRow:1 inComponent:0 animated:YES]; 63 64 69 70 // [self.foods objectAtIndex:0]; == self.foods[0]; 71 // [self.foods[0] count]; 72 73 79 80 //设置一个随机数 81for (int component=0; component) { 82 //获取当前列对应的数据元素的个数 83 int total=[self.foods[component] count]; 84 //根据每一列的总数生成随机数(当前生成的随机数) 85int randomNumber=arc4random()%total; 86 87 //获取当前选中的行(上一次随机后移动到的行) 88 int oldRow=[self.pickerView selectedRowInComponent:0]; 89 90 //比较上一次的行号和当前生成的随机数是否相同,如果相同的话则重新生成 91 while(oldRow==randomNumber) { 92 randomNumber=arc4random()%total; 93 } 94 95 //让pickerview滚动到指定的某一行 96 [self.pickerView selectRow:randomNumber inComponent:component animated:YES]; 97 //模拟,通过代码选中某一行 98 [self pickerView:nil didSelectRow:randomNumber inComponent:component]; 99 }100 }101 102#pragma mark- 设置数据103 //一共多少列104 -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView105 {106 returnself.foods.count;107 }108 109 //每列对应多少行110 -(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component111 {112 //1.获取当前的列113 NSArray *arayM= self.foods[component];114 //2.返回当前列对应的行数115 returnarayM.count;116 }117 118 //每列每行对应显示的数据是什么119 -(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component120 {121 //1.获取当前的列122 NSArray *arayM=self.foods[component];123 //2.获取当前列对应的行的数据124 NSString *name=arayM[row];125 return name;126 }127 128 #pragma mark-设置下方的数据刷新129 //当选中了pickerView的某一行的时候调用130 // 会将选中的列号和行号作为参数传入131 //只有通过手指选中某一行的时候才会调用132 -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component133 {134 //获取对应列,对应行的数据135 NSString *name=self.foods[component][row];136 //赋值137 if (0==component) {138 self.fruitLab.text=name;139 }else if(1==component)140 {141 self.stapleLab.text=name;142}else143 self.drinkLab.text=name;144 }145 146 #pragma mark-隐藏状态栏147 -(BOOL)prefersStatusBarHidden148 {149 return YES;150 }151 @end

四、重要补充

请注意在代码实现中为什么使用 [self.foods[0] count]; 而不是直接使用点语法self.foods[0].count取值。    

[self.foods objectAtIndex:0]; == self.foods[0];//这两句的效果等价,而self调用objectAtIndex:0这个方法,返回的是一个id类型的万能指针,它的真实类型要到实际运行的时候才能检测得到,因此不能直接使用self.foods[0].count。

 

iOS—使用picker View的更多相关文章

  1. iOS开发UI篇—使用picker View控件完成一个简单的选餐应用

    iOS开发UI篇—使用picker View控件完成一个简单的选餐应用 一.实现效果 说明:点击随机按钮,能够自动选取,下方数据自动刷新. 二.实现思路 1.picker view的有默认高度为162 ...

  2. iOS: Assertion failure on picker view

    Q:I'm getting an assertion failure while scrolling a picker view w/ zero data(zero rows). While scro ...

  3. 【IOS笔记】View Controller Basics

    View Controller Basics   视图控制器基础 Apps running on iOS–based devices have a limited amount of screen s ...

  4. ios基础之 view的frame 与 bounds 的区别 (转)

    前言: 学习ios开发有一段时间了,项目也做了两个了,今天看视频,突然发现view的frame和bound两个属性,发现bound怎么也想不明白,好像饶你了死胡同里,经过一番尝试和思考,终于弄明白bo ...

  5. Android 和iOS 中关于View 的一点知识

    View的概念和方法十分重要,这里将对Android 和iOS中出现的,关于视图的一些知识点进行总结,预计文章会比较长,要许多时间慢慢补充. 先转载一部分资料,感谢原作者! 原链接为:http://b ...

  6. 【IOS笔记】View Programming Guide for iOS -1

    原文:View Programming Guide for iOS View and Window Architecture Views and windows present your applic ...

  7. iOS UIKit:view

    @import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css); @import url(/ ...

  8. IOS程序创建view

    在IOS程序中创建view有六种方式 首先创建一个GLViewController类,继承UIViewController. 然后进入GLAppDelegate.m,在- (BOOL)applicat ...

  9. 周记7——ios中picker滑动穿透bug

    Bug描述:使用mint-ui的picker组件时,datepicker和picker在ios的webview(bug是在Hybrid App发现的)中会出现滑动穿透的现象,导致弹层后面的页面也会滚动 ...

随机推荐

  1. jQuery提示组件toastr(取代alert)

    给大家推荐一款jquery提示插件:toastr 它是一个可以取代alert的提示信息框,它在PC,移动设备上都有不错的UI效果. 具体使用方法如下: 1.首先在网页头站调用他需要的js和css文件. ...

  2. 013_RomanToInteger

    #####solution1####faster#### def romanToInt(s): d={ 'I':1, 'V':5, 'X':10, 'L':50, 'C':100, 'D':500, ...

  3. php 默认保几位小数,末尾为0去掉

    计算保留几位小数,末位为0舍去 // 计算 默认保留1位小数 protected function getSprintf($value,$count,$digit = 1) { $num = 0; i ...

  4. ModuleNotFoundError: No module named 'video_back.urls'

    新建Django项目时将settings,urls移除来时报错. 这是我所想要的项目结构  >>>  扁平结构. 将下面这个应用的名字删掉就可以了.

  5. Gulp实战

    推荐文章: gulp.js中文网   :     http://www.gulpjs.com.cn/ DBPOO           :   http://www.dbpoo.com/getting- ...

  6. 【原创】大叔经验分享(46)用户提交任务到yarn报错

    用户提交任务到yarn时有可能遇到下面的错误: 1) Requested user anything is not whitelisted and has id 980,which is below ...

  7. 4.9cf自训9..

    cf401D 状态压缩dp好题,每次把新加入集合的数字放在最后即可 /* 它可以通过重新排列数字n, 它没有任何前导零, x除以m后的余数等于0. 每次把新加的数放在最后 dp[i][j]表示状态i下 ...

  8. Mac App开发

    1. icns制作 在线工具: https://iconverticons.com/online/ 2. 替换dmg图标 选中dmg文件 右键, 选择显示简介 将icns图表拖拽到简介弹出框的左上角图 ...

  9. python计算文件的行数的方法

    1.简单方法把文件读入一个大的列表中,然后统计列表的长度.   count = len(open("文件名").readlines()) print count 2.读取文件某一行 ...

  10. ycmd for emacs 代码自动补全

    YCMD FOR EMACS Table of Contents 1. 安装 1.1. 下载 1.2. 安装相关依赖 1.3. 更新submodules 1.4. 安装 2. 配置 1 安装   1. ...