在餐厅里的点餐系统的核心控件就是UIPickerView

今天晚上在整理以前的项目笔记时,特意把UIPickerView单独拿出来,做了一个简陋的点餐道具。

因为没有素材图片,所有大家将就看看吧

0.用到的主要方法  

- 数据源方法

有多少列
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return _foods.count;
} 第component列有多少行
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
NSArray *array = _foods[component]; return array.count;
} 每行显示什么内容、第component列第row行显示什么文字
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return _foods[component][row];
} - 代理方法
选中了第component列第row行就会调用
// 只有手动选中了某一行才会通知代理
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{ 2.选中某一行
[_pickerView selectRow:index inComponent:component animated:YES];

1.UI界面搭建,将需要用到的控件拖入头文件  

UIPickerView

@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIPickerView *pickerView;
@property (weak, nonatomic) IBOutlet UILabel *fruit;
@property (weak, nonatomic) IBOutlet UILabel *meat;
@property (weak, nonatomic) IBOutlet UILabel *water;
- (IBAction)randomFood:(id)sender; @end

2.初始化数据  

记得实现数据源和代理

<UIPickerViewDataSource, UIPickerViewDelegate>

@interface ViewController () <UIPickerViewDataSource, UIPickerViewDelegate>
{
NSArray *_foods;
}
@end @implementation ViewController - (void)viewDidLoad
{
[super viewDidLoad]; // 1.加载数据
_foods = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"foods.plist" ofType:nil]]; // 2.设置默认值
// _fruit.text = _foods[0][0];
// _meat.text = _foods[1][0];
// _water.text = _foods[2][0];
int count = _foods.count;
for (int i = ; i < count; i++) {
[self pickerView:nil didSelectRow: inComponent:i];
}
}

3.实现数据源方法用于展示数据  

#pragma mark - 数据源
#pragma mark 有多少列
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return _foods.count;
} #pragma mark 第component列有多少行
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
NSArray *array = _foods[component]; return array.count;
} #pragma mark 每行显示什么内容、第component列第row行显示什么文字
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return _foods[component][row];
}

4.实现代理方法,当选定时更新UI界面  

#pragma mark - 代理
#pragma mark 选中了第component列第row行就会调用
// 只有手动选中了某一行才会通知代理
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
// 1.取出选中那行的文字
NSString *text = _foods[component][row]; // 2.显示到对应的label上面
if (component == ) { // 水果
_fruit.text = text;
} else if (component == ) { // 肉
_meat.text = text;
} else { // 饮料
_water.text = text;
}
}

5.实现随机点餐功能  

#pragma mark 随机
- (IBAction)randomFood:(id)sender {
// 1.随机选中第0列的某一行(水果)
// [self randomCompoment:0];
//
// // 2.随机选中第1列的某一行(肉)
// [self randomCompoment:1];
//
// // 3.随机选中第2列的某一行(饮料)
// [self randomCompoment:2]; int count = _foods.count;
for (int i = ; i < count; i++) {
[self randomCompoment:i];
}
} #pragma mark 随机选中某一列的方法
- (void)randomCompoment:(int)component
{
// 0.获得第component列选中的行号
int selectedRow = [_pickerView selectedRowInComponent:component]; // 1.随机生成行号
int index = selectedRow;
while (index == selectedRow) {
index = arc4random_uniform([_foods[component] count]);
} // 一定会生成不一样的行号 // 2.选中某一行
[_pickerView selectRow:index inComponent:component animated:YES]; // 3.更改文字
[self pickerView:nil didSelectRow:index inComponent:component];
}

作者: 清澈Saup
出处:http://www.cnblogs.com/qingche/
本文版权归作者和博客园共有,欢迎转载,但必须保留此段声明,且在文章页面明显位置给出原文连接。

iOS- UIPickerView餐厅点餐系统的更多相关文章

  1. 安卓餐厅点餐系统---针对浩然android工作室的一个小白的分析

    昨天刚把浩然android工作室的下载下来了,为了研究下点餐系统的架构,更好的完成手中的项目,便写出一个分析报告(小白的分析,忘见谅!) 本项目app主要用于餐厅无线订餐使用,功能突出餐厅的订餐需求, ...

  2. 餐厅点餐系统app总结

    总结: 三个冲刺已经结束,虽然没有说十分完美,但该实现的功能还是实现了,只是在市场是相较于专业性的缺乏竞争力,从界面到体验都需进一步优化. 每个人的进度不一样,为了同一个任务需要不断的磨合与合作,但慢 ...

  3. 餐厅点餐系统app第二天

    队友: 郭志豪:http://www.cnblogs.com/gzh13692021053/ 杨子健:http://www.cnblogs.com/yzj666/ 刘森松:http://www.cnb ...

  4. 非常不错的点餐系统应用ios源码完整版

    该源码是一款非常不错的点餐系统应用,应用源码齐全,运行起来非常不错,基本实现了点餐的一些常用的功能,而且界面设计地也很不错,是一个不错的ios应用学习的例子,喜欢的朋友可以下载学习看看,更多ios源码 ...

  5. 餐厅到店点餐系统(APP)

    MY-HR 成员: 角色分配 学号 博客园 丘惠敏 PM项目经理 201406114203 http://www.cnblogs.com/qiuhuimin/ 郭明茵 用户 201406114204 ...

  6. ios 点餐系统

    这个程序的主要界面就是一个TabBarController.总共三个标签,第一个是所有的可点的菜,第二个是已点的菜,第三个是可以留言或者查看所有留言. 下面是第一个页面: 右上角的i按钮是添加新菜,每 ...

  7. 很不错的点餐系统应用ios源代码完整版

    该源代码是一款很不错的点餐系统应用,应用源代码齐全,执行起来很不错,基本实现了点餐的一些经常使用的功能,并且界面设计地也很不错,是一个不错的ios应用学习的样例,喜欢的朋友能够下载学习看看,很多其它i ...

  8. 点餐系统web版功能需求

                餐厅到店点餐系统需求分析 (版本v1.0.0)               成文信息 主题词: 需求分析 作  者: 14商软ETC 文档类别: 审  核: 批  准: 文档性 ...

  9. [课程设计]Scrum 3.8 多鱼点餐系统开发进度(留言反馈系统设计)

    Scrum 3.8 多鱼点餐系统开发进度(留言反馈系统设计) 1.团队名称:重案组 2.团队目标:长期经营,积累客户充分准备,伺机而行 3.团队口号:矢志不渝,追求完美 4.团队选题:餐厅到店点餐系统 ...

随机推荐

  1. yii学习笔记(4),获取请求数据的request组件

    yii在控制器中获取请求数据需要通过request组件来完成 <?php namespace app\controllers; use yii; use yii\web\Controller; ...

  2. day 13 内置函数

    内置函数思维导图:https://www.processon.com/view/link/5c13ad2de4b0ed122da75668 内置函数 作用域相关:   locals() 返回当前作用域 ...

  3. #include stdio.h(A)

    /* 第一个*******知识点工程相关信息******** 1.创建工程 文件->新建->工程->win32 console applecation ->文件名不能为汉字 2 ...

  4. LeetCode初级算法的Python实现--数组

    LeetCode初级算法的Python实现--数组 # -*- coding: utf-8 -*- """ @Created on 2018/6/3 17:06 @aut ...

  5. 成都Uber优步司机奖励政策(4月4日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  6. 网易七鱼 Android 高性能日志写入方案

    本文来自网易云社区 作者:网易七鱼 Android 开发团队 前言 网易七鱼作为一款企业级智能客服系统,对于系统稳定性要求很高,不过难保用户在使用中不会出现问题,而 Android SDK 安装在用户 ...

  7. 一文带你了解 Raft 一致性协议的关键点

    此文已由作者孙建良授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. Raft 协议的发布,对分布式行业是一大福音,虽然在核心协议上基本都是师继 Paxos 祖师爷(lampor ...

  8. sql中的几种连接类型

    一.连接类型简介 在sql中单表查询的几率相对来说比较少,随着数据库的日益复杂,多表关联的情况越来越多,在多表关联的情况下存在多种关联的类型, 1.自关联(join或inner join) 2.左外关 ...

  9. uvaoj455Periodic Strings(枚举)

    A character string is said to have period k if it can be formed by concatenating one or more repetit ...

  10. docker官网安装

    最近发现一些同学居然不会安装docker,难,不难,只是“网络不好”! 如果是学习的话,目前我发现的比较好的方法还是到清华的开源镜像网站: https://mirror.tuna.tsinghua.e ...