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 ...
随机推荐
- 图数据库 Nebula Graph TTL 特性
导读 身处在现在这个大数据时代,我们处理的数据量需以 TB.PB, 甚至 EB 来计算,怎么处理庞大的数据集是从事数据库领域人员的共同问题.解决这个问题的核心在于,数据库中存储的数据是否都是有效的.有 ...
- cmdb客户端代码完善2
目录: 1.面试提问 2.完善采集端代码 3.唯一标识的问题 4.API的验证 1.面试会问到的问题: # 1. 为啥要做CMDB?# - 实现运维自动化, 而CMDB是实现运维自动化的基石# - 之 ...
- Spring Controller单例与线程安全那些事儿
目录 单例(siingleton)作用域 原型(Prototype)作用域 多个HTTP请求在Spring控制器内部串行还是并行执行方法? 实现单例模式并模拟大量并发请求,验证线程安全 附录:Spri ...
- hdu3665Floyd解法
题目链接:http://icpc.njust.edu.cn/Problem/Hdu/3665/ Floyd是经典的dp算法,将迭代过程分成n个阶段,经过n个阶段的迭代所有点对之间的最短路径都可以求出, ...
- Java并发编程之set集合的线程安全类你知道吗
Java并发编程之-set集合的线程安全类 Java中set集合怎么保证线程安全,这种方式你知道吗? 在Java中set集合是 本篇是<凯哥(凯哥Java:kagejava)并发编程学习> ...
- [Flink] Flink的waterMark的通俗理解
导读 Flink 为实时计算提供了三种时间,即事件时间(event time).摄入时间(ingestion time)和处理时间(processing time). 遇到的问题: 假设在一个5秒的T ...
- vue cli3配置开发环境、测试环境、生产(线上)环境
cli3创建vue项目是精简版的少了build和config这2个文件,所以配置开发环境.测试环境.生产环境的话需要自己创建env文件. 需要注意2点: 1.cli2创建项目生成的config文件里的 ...
- Windows下用Python你会几种copy文件的方法?
1. [代码]1. os.system ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 import os import temp ...
- HDU 3303 Harmony Forever 前缀和+树状数组||线段树
Problem Description We believe that every inhabitant of this universe eventually will find a way to ...
- 命令行中运行Java字节码文件提示找不到或无法加载主类的问题
测试类在命令行操作,编译通过,运行时,提示 错误: 找不到或无法加载主类 java类 package com.company.schoolExercise; public class test7_3_ ...