在餐厅里的点餐系统的核心控件就是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. Simple, Poetic, Pithy

    源自:Rob Pike points out Simple, Poetic, Pithy Don't communicate by sharing memory, share memory by co ...

  2. PWA-缓存

    PWA-缓存 基础 PWA强大的离线能力就在于Service Worker拦截请求及提供缓存的能力,Service Worker的缓存能力比较强大,它能够赋予你更加精确控制缓存的能力.示例页面 < ...

  3. 20145202马超《网络对抗》Exp7 网络欺诈技术防范

    本实践的目标理解常用网络欺诈背后的原理,以提高防范意识,并提出具体防范方法.具体有(1)简单应用SET工具建立冒名网站(2)ettercap DNS spoof(3)结合应用两种技术,用DNS spo ...

  4. Oracle,SQL Server 数据库较MySql数据库,Sql语句差异

    原文:Oracle,SQL Server 数据库较MySql数据库,Sql语句差异 Oracle,SQL Server 数据库较MySql数据库,Sql语句差异 1.关系型数据库 百度百科 关系数据库 ...

  5. 北京Uber优步司机奖励政策(12月24日)

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

  6. 单例模式之pymysql运用实例

    何为单例? 简单介绍一下下:单例是个什么鬼东西!!!! 单例模式含义] 单例模式是一种常用的软件设计模式.在它的核心结构中只包含一个被称为单例类的特殊类.通过单例模式可以保证系统中一个类只有一个实例而 ...

  7. VDI数据恢复

    环境:cirtix xendesktop 问题:VDI无法正常启动,后台登录查看报错.多次重启无效果,客户部分数据存放在启动盘. 解决方法:1.创建一台新的VDI(必须保证关机)2.将原有VDI启动盘 ...

  8. redis 学习笔记三

    一.redis 复制 数据库复制指的是发生在不同数据库实例之间,单向的信息传播的行为,通常由被复制方和复制方组成,被复制方和复制方之间建立网络连接,复制方式通常为被复制方主动将数据发送到复制方,复制方 ...

  9. 使用materialization

    explain select `countries`.`id` AS `id`,`countries`.`sortname` AS `sortname`,`countries`.`name` AS ` ...

  10. webpack中Development和Production模式的区分打包

    当我们在开发一个项目的时候,我们一般用development这个环境进行项目的开发,在这个环境下,webpack使用dev-server,帮我们启用一个服务器,然后这个服务器里面还集成了一些,比如hm ...