#import "ViewController.h"

@interface ViewController ()<UIPickerViewDataSource,UIPickerViewDelegate>

/*** 数据数组 ***/
    @property(nonatomic, strong) NSArray * foods;
@property (weak, nonatomic) IBOutlet UILabel *fruitLabel;

@property (weak, nonatomic) IBOutlet UILabel *mainFoodLabel;
@property (weak, nonatomic) IBOutlet UILabel *drinkLabel;

@end

@implementation ViewController

//懒加载
- (NSArray *) foods{
    if(!_foods){
        _foods = [NSArray arrayWithContentsOfFile:[ [NSBundle mainBundle] pathForResource:@"foods.plist" ofType:nil ] ];
    }
    return _foods;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //让label们显示当前的数据
    self.fruitLabel.text = self.foods[0][0];
    self.mainFoodLabel.text = self.foods[1][0];
    self.drinkLabel.text = self.foods[2][0];
}

#pragma mark - <UIPickerViewDataSource, UIPikerViewDelegate>
//返回多少列
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    
    return self.foods.count;
}

//设置每一个component显示多少行
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
  //  NSLog(@"--- pickerView numberOfRowsInComponent");
    NSArray * subArr = self.foods[component];
    return subArr.count;
}

#pragma mark - <代理>
//component列row行显示什么文字
-  (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    NSLog(@"--- pickerView titleForRow");
    return  self.foods[component][row];
}

//监听用户选中
- (void ) pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
   // NSLog(@"%zd的%zd", component, row);
    
    //水果
    if(component == 0 ){
        self.fruitLabel.text = self.foods[component][row];
    }else if( component == 1 ){
    //主菜
       self.mainFoodLabel.text =self.foods[component][row];
    }else if( component == 2){
        //饮料
       self.drinkLabel.text = self.foods[component][row];
    }
    
}

@end



 

 

 

 

知识点:

在XCode中 commond + option + 回车

 轻量级应用开发之(07) UIPickerView使用_轻量级应用开发

 

参考资料:

http://www.cnblogs.com/songliquan/p/4564963.html

http://www.cnblogs.com/qingche/p/3565243.html