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_点菜系统的更多相关文章

  1. AJ学IOS(28)UI之Quartz2D简单介绍

    AJ分享,必须精品 iOS开发UI篇—Quartz2D简单介绍 什么是Quartz2D Quartz 2D是⼀个二维绘图引擎,同时支持iOS和Mac系统 Quartz 2D能完成的工作: 绘制图形 : ...

  2. AJ学IOS(13)UI之UITableView学习(下)汽车名牌带右侧索引

    AJ分享,必须精品 先看效果图 代码 ViewController #import "NYViewController.h" #import "NYCarGroup.h& ...

  3. AJ学IOS 之微博项目实战(2)微博主框架-自定义导航控制器NavigationController

    AJ分享,必须精品 一:添加导航控制器 上一篇博客完成了对底部的TabBar的设置,这一章我们完成自定义导航控制器(NYNavigationController). 为啥要做自定义呢,因为为了更好地封 ...

  4. AJ学IOS(01) UI之Hello World与加法计算器

    不多说,AJ分享,必须精品 这两个一个是HelloWorld(左边) 另一个是 加法计算器(右边)的截图. 先运行第一个 程序看看效果 1.打开Xcode(没有哦mac系统的没有xcode的帮你们默哀 ...

  5. AJ学IOS(42)UI之核心动画CAAnimationGroup以及其他

    AJ分享,必须精品 效果: 代码: 很简单,不多说,就是把一堆动画放一起,看代码. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent * ...

  6. AJ学IOS(23)UI之控制器管理

    AJ分享,必须精品 控制器以及view的多种创建方式 控制器view的加载 通过storyboard创建 1:先加载storyboard⽂件(Test是storyboard的⽂文件名) UIStory ...

  7. AJ学IOS(22)UI之UIApplicationDelegate和UIWindow

    AJ分享,必须精品 UIApplicationDelegate 每次新建完项目,都有个带有“AppDelegate”字眼的类,它就是UIApplication的代理 NYAppDelegate默认已经 ...

  8. AJ学IOS(17)UI之纯代码自定义Cell实现新浪微博UI

    AJ分享,必须精品 先看效果图 编程思路 代码创建Cell的步骤 1> 创建自定义Cell,继承自UITableViewCell 2> 根据需求,确定控件,并定义属性 3> 用get ...

  9. AJ学IOS(08)UI之热门_喜马拉雅UI实现-UIScrollView的使用

    AJ分享,必须精品 先看效果 storyBoard用到的控件 代码实现 */ // // NYViewController.m // 05 - 喜马拉雅 // // Created by apple ...

随机推荐

  1. Java8 Stream流

    第三章 Stream流 <Java8 Stream编码实战>的代码全部在https://github.com/yu-linfeng/BlogRepositories/tree/master ...

  2. 13.unittest扩展

  3. 记录一次云主机部署openstack的血泪史

    看见这个部署成功的留下了激动的泪水 经过长时间的BUG苦肝终于成功部署成功  部署的环境2vCPU 8GB 阿里云主机,部署成功以后内存占用确实蛮高的 记录这一次踩坑,给后来者避免踩坑时间,个人踩坑踩 ...

  4. 第十七周Java实验作业

    实验十七  线程同步控制 实验时间 2018-12-10 1.实验目的与要求 (1) 掌握线程同步的概念及实现技术: 多线程并发运行不确定性问题解决方案:引入线程同步机制,使得另一线程使用该方法,就只 ...

  5. 第十六周Java实验作业

    实验十六  线程技术 实验时间 2017-12-8 1.实验目的与要求 (1) 掌握线程概念: 多线程是进程执行过程中产生的多条执行线索,线程是比进程执行更小的单位. 线程不能独立存在,必须存在于进程 ...

  6. laravel中间件的创建思路分析

    网上有很多解析laravel中间件的实现原理,但是不知道有没有读者在读的时候不明白,作者是怎么想到要用array_reduce函数的? 本文从自己的角度出发,模拟了如果我是作者,我是怎么实现这个中间件 ...

  7. 如何使用WordPress搭建网站

    1.空间的申请 阿里用户可以申请[阿里共享虚拟主机普惠版6元/年],虽然配置和空间不高,但也可以做个小站点的.当不满足当前配置的时候,随时可以进行升级,所以拿来练手还是比较合适的.   2.WordP ...

  8. python绘图设置标题、标签,无法显示中文

    先说解决办法:在程序开始之前,引入使用的模块之后,添加如下代码: plt.rcParams['font.sans-serif']=['SimHei'] plt.rcParams['axes.unico ...

  9. CF 997A

    You’ve got a string a1,a2,…,an, consisting of zeros and ones.Let’s call a sequence of consecutive el ...

  10. [React]Context机制

    在React中,Context机制是为了方便在组件树间传递数据. 例子 import React from 'react' const themes={ light:"亮色主题", ...