首页 技术 正文
技术 2022年11月23日
0 收藏 828 点赞 2,599 浏览 2133 个字

2016-1-3点菜系统demo的实现,pickerView的学习

//
// ViewController.m
// pickView
//
// Created by Mac on 16/1/3.
// Copyright © 2016年 Mac. All rights reserved.
//#import "ViewController.h"@interface ViewController ()<UIPickerViewDataSource,UIPickerViewDelegate>
@property (weak, nonatomic) IBOutlet UIPickerView *pickView;
@property (nonatomic, strong)NSArray *foods;
@property (weak, nonatomic) IBOutlet UILabel *fruitLable;
@property (weak, nonatomic) IBOutlet UILabel *mainfoodLable;
@property (weak, nonatomic) IBOutlet UILabel *drinkLable;
- (IBAction)randomPick;@end@implementation ViewController
- (NSArray *)foods
{
if (!_foods) {
NSString *path = [[NSBundle mainBundle] pathForResource:@"foods.plist" ofType:nil];
NSArray *array = [NSArray arrayWithContentsOfFile:path];
_foods = array;
}
return _foods;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self pickerView:self.pickView didSelectRow: inComponent:];
[self pickerView:self.pickView didSelectRow: inComponent:];
[self pickerView:self.pickView didSelectRow: inComponent:];}
#pragma mark - 数据源方法
//返回总共多少列;
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return self.foods.count;
}
//返回每列多收行;
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
NSArray *array = self.foods[component];
return array.count;
}
#pragma mark - 代理方法
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
NSArray *array = self.foods[component];
return array[row];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
// NSLog(@"component is %d,row is %d",component,row);
NSArray *array = self.foods[component];
NSString *str = array[row];
switch (component) {
case :
self.fruitLable.text = str;
break;
case :
self.mainfoodLable.text = str;
break;
case :
self.drinkLable.text = str;
break;
default:
break;
}
}
#pragma mark - 随机点菜按钮
- (IBAction)randomPick {
// 找出总共多少列
NSInteger countOfColumns = self.foods.count;
// 利用for循环找出每一列有多少行
for (int i = ; i < countOfColumns ; i ++) {
NSArray *array = self.foods[i];
// 找出当前这一列有多少行;
NSInteger countOfRows = array.count;
// 在这一列中随机产生一个数字;
NSUInteger randomRow = arc4random_uniform((int)countOfRows);
// 更改数据
[self pickerView:self.pickView didSelectRow:randomRow inComponent:i];
[self.pickView selectRow:randomRow inComponent:i animated:YES];
}
}
@end
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,254
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,692
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,531
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,302
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,942
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,102