需求:
1.显示3列餐点(水果、主菜、饮料)的数据,供选择;
2.选择完餐点后并显示在界面上;
3.提供“随机”按钮,随机选择餐点搭配,让上天决定选择什么餐点;
效果图:
实现步骤:
1、搭建界面;
2、实现UIPikcerView的代理方法;
3、响应选择事件;
4、完成显示选择餐点的数据以及随机选择的功能;
具体代码:
1 // 2 // ViewController.m 3 // 12-27-UIPickerView 4 // 5 // Created by xiaomoge on 14/12/27. 6 // Copyright (c) 2014年 xiaomoge. All rights reserved. 7 // 8 9 #import "ViewController.h" 10 11 @interface ViewController ()<UIPickerViewDataSource,UIPickerViewDelegate> 12 13 @property (nonatomic,strong)NSArray *foods; 14 //水果标签 15 @property (weak, nonatomic) IBOutlet UILabel *fruitLabel; 16 //主食标签 17 @property (weak, nonatomic) IBOutlet UILabel *mainFoodLabel; 18 //饮料标签 19 @property (weak, nonatomic) IBOutlet UILabel *drinkLabel; 20 //随机按钮事件 21 - (IBAction)randomMenu; 22 @property (weak, nonatomic) IBOutlet UIPickerView *pickerView; 23 24 @end 25 26 @implementation ViewController 27 28 #pragma mark - 懒加载 29 -(NSArray *)foods{ 30 if (!_foods) { 31 NSString *filePath = [[NSBundle mainBundle] pathForResource:@"foods.plist" ofType:nil]; 32 _foods = [NSArray arrayWithContentsOfFile:filePath]; 33 } 34 return _foods; 35 } 36 37 - (void)viewDidLoad { 38 [super viewDidLoad]; 39 40 //默认选中数据,第一行数据 41 NSInteger columns = self.foods.count; 42 for (int i = 0; i < columns; i++) { 43 [self pickerView:nil didSelectRow:0 inComponent:i]; 44 } 45 } 46 #pragma mark - UIPickerView数据源 47 //列数 48 - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{ 49 return self.foods.count; 50 } 51 //每一列的行数 52 - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{ 53 //获取每一列的数据 54 NSArray *items = self.foods[component]; 55 //返回每一列的个数 56 return items.count; 57 } 58 #pragma mark - UIPickerView代理 59 -(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{ 60 //获取每一列的数据 61 NSArray *items = self.foods[component]; 62 //返回对应列的行的数据 63 return items[row]; 64 } 65 #pragma mark - pickerView的选中方法 66 -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{ 67 //获取数据 68 NSArray *items = self.foods[component]; 69 NSString *rowStr = items[row]; 70 //更改数据 71 switch (component) { 72 case 0://水果 73 self.fruitLabel.text = rowStr; 74 break; 75 case 1://主食 76 self.mainFoodLabel.text = rowStr; 77 break; 78 case 2://饮料 79 self.drinkLabel.text = rowStr; 80 break; 81 default: 82 break; 83 } 84 } 85 #pragma mark 随机菜单,由天决定我们吃什么 86 - (IBAction)randomMenu { 87 //获取每列有多少行 88 NSInteger columns = self.foods.count; 89 90 for (int i = 0; i < columns; i ++) { 91 NSArray *items = self.foods[i]; 92 //对应列的行数 93 NSInteger rowsInColumn = items.count; 94 //在行范围产生随机数据 95 NSInteger randomRow =arc4random_uniform((int)rowsInColumn); 96 // 获取旧的行数 97 NSInteger oldRow = [self.pickerView selectedRowInComponent:i]; 98 99 while (oldRow == randomRow ) { 100 randomRow =arc4random_uniform((int)rowsInColumn);//如果随机出来的数据和旧数据相同,那么继续进行随机 101 } 102 //更改label的数据 103 [self pickerView:nil didSelectRow:randomRow inComponent:i]; 104 //更改UIPickerView选中的数据 105 [self.pickerView selectRow:randomRow inComponent:i animated:YES]; 106 } 107 } 108 @end
注意点:要改变UIPikcerView选中的行,要使用UIPikcerView的一个方法:
1 - (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated;