AJ学IOS(20)UI之UIPickerView_点菜系统
AJ分享,必须精品
先看效果图 ##
UIPickerView控件
UIPickerView用处:
用来展示很多行(row) 很多列(component )的数据,多用于电子商务的点菜,城市选择等等。
UIPickerView用法:
他用起来跟tableView差不多,用法:
1:设置代理和数据源
@interface NYViewController ()<UIPickerViewDataSource, UIPickerViewDelegate>
数据源:UIPickerViewDataSource,
1,返回有多少列
2,返回有多少行
#pragma mark - UIPickerViewDataSource
// 返回pickerView一共有多少列
- (NSInteger) numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
// return 3;
return self.foods.count;
}
// 返回pickerView的第component列有多少行
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
// return 4;
// 1.获取对应列的数组
NSArray *subFoods = self.foods[component];
// 2.返回对应列的行数
return subFoods.count;
}
代理UIPickerViewDelegate
返回第component列的第row行显示什么内容
#pragma mark - UIPickerViewDelegate
// 返回第component列的第row行显示什么内容
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
// 1.获取对应列的数组
NSArray *subFoods = self.foods[component];
// 2.获取对应行的标题
NSString *name = subFoods[row];
return name;
}
怎么监听选中哪一行
didSelectRow
当我们选中某一列某一行的时候, 我们就把相应的数据设置。
// 当选中了pickerView的某一行的时候调用
// 会将选中的列号和行号作为参数传入
// 只有通过手指选中某一行的时候才会调用
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
// NSLog(@"component = %d, row = %d", component, row);
// 1.获取对应列对应行的数据
NSString *name = self.foods[component][row];
// NSLog(@"name = %@", name);
// 2.判断选择的是哪一列, 根据列号设置对应的数据
if (0 == component) {
// 水果
self.fruitLabel.text = name;
}else if (1 == component)
{
// 主菜
self.stapleLabel.text = name;
}else
{
// 饮料
self.drinkLabel.text = name;
}
}
实现随机事件
如何让pickerView自己滚动到哪一行
selectRow inComponent让pickerView主动的滚动到某一列某一行
- (IBAction)randomFood:(UIButton *)sender {
// 让pickerView主动选中某一行
// 让pickerView选中inComponent列的Row行
// [self.pickerView selectRow:1 inComponent:0 animated:YES];
/*
[self.pickerView selectRow: arc4random() % 12 inComponent:0 animated:YES];
[self.pickerView selectRow: arc4random() % 15 inComponent:1 animated:YES];
[self.pickerView selectRow: arc4random() % 10 inComponent:2 animated:YES];
*/
// [self.foods objectAtIndex:0]; == self.foods[0];
// [self.foods[0] count];
/*
// 根据每一列的元素个数生成随机值
[self.pickerView selectRow: arc4random() % [self.foods[0] count] inComponent:0 animated:YES];
[self.pickerView selectRow: arc4random() % [self.foods[1] count] inComponent:1 animated:YES];
[self.pickerView selectRow: arc4random() % [self.foods[2] count] inComponent:2 animated:YES];
*/
for (int component = 0; component < self.foods.count; component++) {
// 获取对应列的数据总数
int total = [self.foods[component] count];
// 根据每一列的总数生成随机数(当前生成的随机数)
int randomNumber = arc4random() % total;
// 获取当前选中的行(上一次随机后移动到的行)
int oldRow = [self.pickerView selectedRowInComponent:0];
// NSLog(@"oldRow = %d", oldRow);
// 比较上一次的行号和当前生成的随机数是否相同, 如果相同重新生成
while (oldRow == randomNumber) {
randomNumber = arc4random() % total;
}
// 让pickerview滚动到某一行
[self.pickerView selectRow: randomNumber inComponent:component animated:YES];
// 通过代码选中某一行
[self pickerView:nil didSelectRow:randomNumber inComponent:component];
}
}
AJ学IOS(20)UI之UIPickerView_点菜系统的更多相关文章
- AJ学IOS(28)UI之Quartz2D简单介绍
AJ分享,必须精品 iOS开发UI篇—Quartz2D简单介绍 什么是Quartz2D Quartz 2D是⼀个二维绘图引擎,同时支持iOS和Mac系统 Quartz 2D能完成的工作: 绘制图形 : ...
- AJ学IOS(13)UI之UITableView学习(下)汽车名牌带右侧索引
AJ分享,必须精品 先看效果图 代码 ViewController #import "NYViewController.h" #import "NYCarGroup.h& ...
- AJ学IOS 之微博项目实战(2)微博主框架-自定义导航控制器NavigationController
AJ分享,必须精品 一:添加导航控制器 上一篇博客完成了对底部的TabBar的设置,这一章我们完成自定义导航控制器(NYNavigationController). 为啥要做自定义呢,因为为了更好地封 ...
- AJ学IOS(01) UI之Hello World与加法计算器
不多说,AJ分享,必须精品 这两个一个是HelloWorld(左边) 另一个是 加法计算器(右边)的截图. 先运行第一个 程序看看效果 1.打开Xcode(没有哦mac系统的没有xcode的帮你们默哀 ...
- AJ学IOS(42)UI之核心动画CAAnimationGroup以及其他
AJ分享,必须精品 效果: 代码: 很简单,不多说,就是把一堆动画放一起,看代码. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent * ...
- AJ学IOS(23)UI之控制器管理
AJ分享,必须精品 控制器以及view的多种创建方式 控制器view的加载 通过storyboard创建 1:先加载storyboard⽂件(Test是storyboard的⽂文件名) UIStory ...
- AJ学IOS(22)UI之UIApplicationDelegate和UIWindow
AJ分享,必须精品 UIApplicationDelegate 每次新建完项目,都有个带有“AppDelegate”字眼的类,它就是UIApplication的代理 NYAppDelegate默认已经 ...
- AJ学IOS(17)UI之纯代码自定义Cell实现新浪微博UI
AJ分享,必须精品 先看效果图 编程思路 代码创建Cell的步骤 1> 创建自定义Cell,继承自UITableViewCell 2> 根据需求,确定控件,并定义属性 3> 用get ...
- AJ学IOS(08)UI之热门_喜马拉雅UI实现-UIScrollView的使用
AJ分享,必须精品 先看效果 storyBoard用到的控件 代码实现 */ // // NYViewController.m // 05 - 喜马拉雅 // // Created by apple ...
随机推荐
- LeetCode | 287. 寻找重复数
特别感谢LeetCode大佬陈牧远的科普知识 给定一个包含 n + 1 个整数的数组 nums,其数字都在 1 到 n 之间(包括 1 和 n),可知至少存在一个重复的整数.假设只有一个重复的整数,找 ...
- python基础知识8——常见内置模块
Python之路-python(常用模块学习) 模块介绍 time &datetime模块 random os sys shutil shelve xml处理 yaml处理 configpar ...
- asp.net net::ERR_ABORTED 500 (Internal Server Error) 无法加载JS CSS等文件的解决方法
网站换服务器,部署上去后打开首页,js .css等文件始终无法加载,经过排查,问题出现在web.config文件中. <defaultDocument> <files> < ...
- leetcode 每日签到 409. 最长回文串
题目: 最长回文串 给定一个包含大写字母和小写字母的字符串,找到通过这些字母构造成的最长的回文串. 在构造过程中,请注意区分大小写.比如 "Aa" 不能当做一个回文字符串. 注意: ...
- 爬虫&Selenium&ChromeDriver
一.Selenium selenium是什么 Selenium [1] 是一个用于Web应用程序测试的工具.Selenium测试直接运行在浏览器中,就像真正的用户在操作一样.支持的浏览器包括IE(7, ...
- npm git 常用命令行 记录
1. 推出node命令行: 两次ctrl+C或者一次ctrl+D 退出终端:exit; 2.npm 常用 npm install <name> 安装包 npm install &l ...
- 朴素贝叶斯分类器(Naive Bayesian Classifier)
本博客是基于对周志华教授所著的<机器学习>的"第7章 贝叶斯分类器"部分内容的学习笔记. 朴素贝叶斯分类器,顾名思义,是一种分类算法,且借助了贝叶斯定理.另外,它是一种 ...
- Python第六章-函数02-函数的作用域
函数 三.作用域规则 有了函数之后,我们必须要面对一个作用域的问题. 比如:你现在访问一个变量,那么 python 解析器是怎么查找到这个变量,并读取到这个变量的值的呢? 依靠的就是作用域规则! 3. ...
- ContOS7中使用Nginx进行TCP反向代理
一.安装Nginx 1.下载:http://nginx.org/en/download.html wget http://nginx.org/download/nginx-1.16.1.tar.gz ...
- ICCV 2019|70 篇论文抢先读,含目标检测/自动驾驶/GCN/等(提供PDF下载)
虽然ICCV2019已经公布了接收ID名单,但是具体的论文都还没放出来,为了让大家更快得看论文,我们汇总了目前已经公布的大部分ICCV2019 论文,并组织了ICCV2019论文汇总开源项目(http ...