ios pickerView 点菜

//
//  ViewController.m
//  01-点菜系统
//
//  Created by panba on 16-4-12.
//  Copyright (c) 2016年 panba. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UIPickerViewDataSource,UIPickerViewDelegate>
@property(nonatomic,strong)NSArray *foods;
@property(nonatomic,strong)UIPickerView *pickView;
@property(nonatomic,weak) UILabel *label4;
@property(nonatomic,weak) UILabel *label5;
@property(nonatomic,weak) UILabel *label6;

@end

@implementation ViewController

//1-懒加载
-(NSArray *)foods
{
    if (_foods == nil) {
        //1-找到路径
        NSString *fullPath = [[NSBundle mainBundle]pathForResource:@"foods.plist" ofType:nil];
        _foods = [NSArray arrayWithContentsOfFile:fullPath];
    }
    return _foods;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    //1-顶部视图
    UIView *headerView = [[UIView alloc]init];
    headerView.frame = CGRectMake(0, 0, 320, 64);
    headerView.backgroundColor = [UIColor blueColor];
    [self.view addSubview:headerView];
    //1.1增加按钮
    UIButton *rodomBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rodomBtn.frame = CGRectMake(20, 20, 64, 20);
    [rodomBtn setTitle:@"随机" forState:UIControlStateNormal];
    [rodomBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [rodomBtn addTarget:self action:@selector(randomFood) forControlEvents:UIControlEventTouchUpInside];
    [headerView addSubview:rodomBtn];
    //1.2软件label
    UILabel *titleLabel = [[UILabel alloc]init];
    titleLabel.frame = CGRectMake(140, 20, 100, 20);
    titleLabel.tintColor = [UIColor blackColor];
    titleLabel.text = @"点菜系统";
    [headerView addSubview:titleLabel];
    //2-设置中部视图
    UIView *middleView = [[UIView alloc]init];
    middleView.frame = CGRectMake(0, 64, 320, 216);
    middleView.backgroundColor = [UIColor grayColor];
    [self.view addSubview:middleView];
    //2.1增加pickview
    UIPickerView *pickView = [[UIPickerView alloc]init];
    pickView.frame = CGRectMake(0, 0, 320, 216);
    [middleView addSubview:pickView];
    pickView.dataSource = self;
    pickView.delegate = self;
    self.pickView = pickView;
    //3-底部视图
    UIView *bottomView = [[UIView alloc]init];
    bottomView.frame = CGRectMake(0, 280, 320, 280);
    bottomView.backgroundColor = [UIColor redColor];
    [self.view addSubview:bottomView];
    //3.1添加几个label
    UILabel *lable1 = [[UILabel alloc]initWithFrame:CGRectMake(20, 20, 40, 20)];
    UILabel *lable2 = [[UILabel alloc]initWithFrame:CGRectMake(20, 80, 40, 20)];
    UILabel *lable3 = [[UILabel alloc]initWithFrame:CGRectMake(20, 140, 40, 20)];
    UILabel *lable4 = [[UILabel alloc]initWithFrame:CGRectMake(100, 20, 100, 20)];
    UILabel *lable5 = [[UILabel alloc]initWithFrame:CGRectMake(100, 80, 100, 20)];
    UILabel *lable6 = [[UILabel alloc]initWithFrame:CGRectMake(100, 140, 100, 20)];
    lable1.text = @"水果";
    lable2.text = @"主菜";
    lable3.text = @"饮料";
    lable1.textColor = [UIColor blackColor];
    lable2.textColor = [UIColor blackColor];
    lable3.textColor = [UIColor blackColor];
    lable4.textColor = [UIColor blackColor];
    lable5.textColor = [UIColor blackColor];
    lable6.textColor = [UIColor blackColor];
    self.label4 = lable4;
    self.label5 = lable5;
    self.label6 = lable6;
    [bottomView addSubview:lable1];
    [bottomView addSubview:lable2];
    [bottomView addSubview:lable3];
    [bottomView addSubview:lable4];
    [bottomView addSubview:lable5];
    [bottomView addSubview:lable6];
    
    //设置pickview的默认选择行数
    for (int component = 0; component < self.foods.count; component++) {
        
    }
}
//2-设置pickview返回多少组
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return self.foods.count;
}
//2.1设置每一组返回多少行
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    NSArray *foodsModel = self.foods[component];
    return foodsModel.count;
}
//2.3设置每一组的每一行都显示什么内容
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    NSArray *foodsModel = self.foods[component];
    NSString *title = foodsModel[row];
    return title;
}
//3-重写didselect方法
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    NSString *name = self.foods[component][row];
    if (component ==0) {
        self.label4.text = name;
    }else if (component == 1)
    {
        self.label5.text = name;
    }else if (component ==2)
    {
        self.label6.text = name;
    }
    
}
//3-重新随机都方法
-(void)randomFood
{
    for (int component = 0; component <self.foods.count; component++) {
        // 获取对应列的数据总数
        int total = [self.foods[component] count];
        // 根据每一列的总数生成随机数(当前生成的随机数)
        int randomNumber = arc4random() % total;
        // 获取当前选中的行(上一次随机后移动到的行)
        int oldRow = [self.pickView selectedRowInComponent:0];
        while (randomNumber ==  oldRow) {
            randomNumber = arc4random() % total;
        }
        // 让pickerview滚动到某一行
        [self.pickView selectRow:randomNumber inComponent:component animated:YES];
        //
        [self pickerView:nil didSelectRow:randomNumber inComponent:component];
    }
}

@end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Y型树杈子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值