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 ...
随机推荐
- centos 7 中没有iptables 和service iptables save 指令使用失败问题解决方案
1.任意运行一条iptables防火墙规则配置命令: iptables -P OUTPUT ACCEPT 2.对iptables服务进行保存: service iptables save 如果上述命令 ...
- linux入门系列18--Web服务之Apache服务2
接上一篇文章,在了解Apache基本配置以及SELinux相关知识后,继续演示Apache提供的虚拟主机功能以及访问控制方式. 如果还没看上一篇的建议先查看后再来,上篇文章"linux入门系 ...
- 原来rollup这么简单之 rollup.watch篇
大家好,我是小雨小雨,致力于分享有趣的.实用的技术文章. 内容分为翻译和原创,如果有问题,欢迎随时评论或私信,希望和大家一起进步. 大家的支持是我创作的动力. 计划 rollup系列打算一章一章的放出 ...
- Model、Form、ModelForm的比较
Model.Form.ModelForm 本节内容: 1:Model 2:Form 3:Model Form 1 2 3 http://www.cnblogs.com/wupeiqi/articles ...
- TensorFlow 多元线性回归【波士顿房价】
1数据读取 1.1数据集解读 1.2引入包 %matplotlib notebook import tensorflow as tf import matplotlib.pyplot as plt i ...
- 题解 P1002 【过河卒】
正文 简单描述一下题意: 士兵想要过河,他每一次可以往下走一格,也可以往右走一格,但马一步走到的地方是不能走的,问走到\(n\)行,\(m\)列有多少种走法 我们显然应该先根据马的位置将不能走的格子做 ...
- windows找不到文件gpedit.msc处理方法
新建一个txt,输入 @echo offpushd "%~dp0"dir /b C:\Windows\servicing\Packages\Microsoft-Windows-Gr ...
- 【Springboot】实例讲解Springboot整合OpenTracing分布式链路追踪系统(Jaeger和Zipkin)
1 分布式追踪系统 随着大量公司把单体应用重构为微服务,对于运维人员的责任就更加重大了.架构更复杂.应用更多,要从中快速诊断出问题.找到性能瓶颈,并不是一件容易的事.因此,也随着诞生了一系列面向Dev ...
- .Net 微服务架构技术栈的那些事
一.前言 大家一直都在谈论微服务架构,园子里面也有很多关于微服务的文章,前几天也有一些园子的朋友问我微服务架构的一些技术,我这里就整理了微服务架构的技术栈路线图,这里就分享出来和大家一起探讨学习,同时 ...
- pycharm 秘籍:快捷键技巧等
Pycharm基本使用 安装 下载地址:https://www.jetbrains.com/pycharm/download 选择Professional 专业版 Comunnity社区版是免费的,但 ...