点菜系统 pickview的简单实用

使用pickview的时候多想想tableview的使用,观察两者的相同之处

pickview的主要用途用于选择地区  生日年月日  和点餐

示例代码 简单的pickview点餐系统
//  ViewController.m
//  UIPickView

#import "ViewController.h"

@interface ViewController ()<UIPickerViewDelegate>
@property (strong, nonatomic) IBOutlet UIPickerView *pickView;

@property (nonatomic,strong)NSArray *foods;
@property (strong, nonatomic) IBOutlet UILabel *fruit;

@property (strong, nonatomic) IBOutlet UILabel *food;
@property (strong, nonatomic) IBOutlet UILabel *drink;

@end

@implementation ViewController



- (IBAction)random:(id)sender {

    //com +alt +[ 代码上跳
    //com + [ 代码左移动
    
    for (int i = 0; i <self.foods.count; i++) {
        int count =(int)[self.foods[i] count];
        int randomNum = arc4random_uniform(count);

          //点击pickview每一列随机选中一行
        [_pickView selectRow:randomNum inComponent:i animated:YES];
        //每次随机之后调用下面方法为label复制

       //随机选中的文字展示到label
        [self pickerView:nil didSelectRow:randomNum inComponent:i];
        

    }

}

- (NSArray *)foods{
    if (_foods ==nil) {
    NSString *path =  [[NSBundle mainBundle]pathForResource:@"foods.plist" ofType:nil];
        
        NSArray *array = [NSArray arrayWithContentsOfFile:path];
        //大数组
        _foods = array;
 
    }
   return  _foods;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.pickView.delegate = self;
    //初始化label的标题
    //设置label的默认值 都是第0行
    for (int i = 0; i <3; i++) {
        [self pickerView:self.pickView didSelectRow:0 inComponent:i];
    }

}
//返回pickview有多少列
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return self.foods.count;
}
//返回每列有多少行
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return [self.foods[component] count];
}

//返回第component列的每一行的行高
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component __TVOS_PROHIBITED{
    return 60;
}


//返回第component第row行的标题

- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component __TVOS_PROHIBITED{
    
    NSArray *array = self.foods[component];
    return array[row];

    
}
//富文本属性  描述文字额大小和颜色等等
//- (nullable NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component NS_AVAILABLE_IOS(6_0) __TVOS_PROHIBITED;


//如果同时返回字符串和view的方法 返回uiview的优先级别比较高
//返回第component列第row行的view
//- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(nullable UIView *)view __TVOS_PROHIBITED{
//    UIView *v = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 60, 60)];
//    v.backgroundColor = [UIColor redColor];
//    return v;
//}


//选中第component第row行调用
//__func__:返回当前方法在哪个类里面调用
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component __TVOS_PROHIBITED{
    
    
    switch (component) {
        case 0:
            
            _fruit.text = self.foods[component][row];
            
            
            
            break;
            
        case 1:
            
            _food.text = self.foods[component][row];
            
            break;
            
        case 2:
            _drink.text = self.foods[component][row];
            
            break;
            
        default:
            break;
    }
    
}

@end

posted @ 2016-01-31 14:27  听风观雨阁  阅读(323)  评论(0编辑  收藏  举报