您的位置:首页 > 产品设计 > UI/UE

iOS之UIPickerView使用(一)

2017-04-20 20:20 411 查看
#import "ViewController.h"

@interface
ViewController () <UIPickerViewDataSource,UIPickerViewDelegate>

@property (weak,nonatomic)
IBOutletUIPickerView *pickerView;

// 水果

@property(nonatomic,weak)
IBOutletUILabel *fruitLbl;

// 主菜

@property(nonatomic,weak)
IBOutletUILabel *mainFoodLbl;

// 酒水

@property(nonatomic,weak)
IBOutletUILabel *drinkLbl;

@property (nonatomic,strong)
NSArray *foods;

@end

@implementation ViewController

- (void)viewDidLoad {

    [superviewDidLoad];

    

    // 随机色

//    [UIColor colorWithRed:arc4random_uniform(256) / 255.0 green:arc4random_uniform(256) / 255.0 blue:arc4random_uniform(256) / 255.0 alpha:1.0];

    

//    [self pickerView:self.pickerView didSelectRow:0 inComponent:0];

//    [self pickerView:self.pickerView didSelectRow:0 inComponent:1];

//    [self pickerView:self.pickerView didSelectRow:0 inComponent:2];

    

    

    //让label启动就显示

    for (int i =0; i <
self.foods.count; i++) {

        

        [selfpickerView:self.pickerViewdidSelectRow:0inComponent:i];

    }

}

#pragma mark - 点击随机点餐按钮的时候调用

- (IBAction)randomBtnClick:(UIButton *)sender {

    

    for (int i =0; i <
self.foods.count; i++) {

     

        // 1.让pickerView随机选中

        // 生成随机数

        // 第i组对应的范围

        

        // 10  11 0~10

        NSInteger count = [self.foods[i]count];

        u_int32_t randomNumber =arc4random_uniform((int)count);

        

        // 1.2每次的数据都不一样

        // 获取当前列选中的行号

        NSInteger selRow = [self.pickerViewselectedRowInComponent:i];

        

        // 如果行号相等,重新生成

        while ((int)selRow == randomNumber) {

            randomNumber = arc4random_uniform((int)count);

        }

        

        [self.pickerViewselectRow:randomNumber
inComponent:i animated:YES];

        

        

        // 2.让label改变

        [selfpickerView:self.pickerViewdidSelectRow:randomNumberinComponent:i];

    }

    

}

#pragma mark - 代理方法

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row
inComponent:(NSInteger)component {

    // 1.获取选中的数据

    NSString *selFood =self.foods[component][row];

    

    // 2.设置给label

    if (component ==0) {

        self.fruitLbl.text = selFood;

    } elseif (component ==
1) {

        self.mainFoodLbl.text = selFood;

    } else {

        self.drinkLbl.text = selFood;

    }

    

}

// 返回每一行显示的内容

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row
forComponent:(NSInteger)component {

   
// 获取每一组的数据

    NSArray *group =self.foods[component];

    

   
// 返回每一行显示的内容

    return group[row];

}

#pragma mark - 数据源方法

// 返回有多少列"组"

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {

    

    returnself.foods.count;

    

}

// 返回每一组有多少行

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{

    

   
// 获取每一组的所有数据的数组

    NSArray *group =self.foods[component];

    

    // 返回行数

    return group.count;

}

#pragma mark - 懒加载

- (NSArray *)foods {

    if (_foods ==nil) {

        

        // 1.找到文件路径

        NSString *filePath = [[NSBundlemainBundle]
pathForResource:@"01foods.plist"ofType:nil];

        

        // 2.转为数组

        _foods = [NSArrayarrayWithContentsOfFile:filePath];

        

    }

    return_foods;

    

}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: