标签:
// // 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:0 inComponent:0]; [self pickerView:self.pickView didSelectRow:0 inComponent:1]; [self pickerView:self.pickView didSelectRow:0 inComponent:2]; } #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 0: self.fruitLable.text = str; break; case 1: self.mainfoodLable.text = str; break; case 2: self.drinkLable.text = str; break; default: break; } } #pragma mark - 随机点菜按钮 - (IBAction)randomPick { // 找出总共多少列 NSInteger countOfColumns = self.foods.count; // 利用for循环找出每一列有多少行 for (int i = 0; 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
2016-1-3点菜系统demo的实现,pickerView的学习
标签:
原文地址:http://www.cnblogs.com/BJTUzhengli/p/5097279.html