码迷,mamicode.com
首页 > 其他好文 > 详细

2016-1-3点菜系统demo的实现,pickerView的学习

时间:2016-01-03 22:17:52      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:

技术分享

//
//  ViewController.m
//  pickView
//
//  Created by Mac on 16/1/3.
//  Copyright © 2016年 Mac. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UIPickerViewDataSource,UIPickerViewDelegate>
@property (weak, nonatomic) IBOutlet UIPickerView *pickView;
@property (nonatomic, strong)NSArray *foods;
@property (weak, nonatomic) IBOutlet UILabel *fruitLable;
@property (weak, nonatomic) IBOutlet UILabel *mainfoodLable;
@property (weak, nonatomic) IBOutlet UILabel *drinkLable;
- (IBAction)randomPick;

@end

@implementation ViewController
- (NSArray *)foods
{
    if (!_foods) {
    NSString *path =  [[NSBundle mainBundle] pathForResource:@"foods.plist" ofType:nil];
        NSArray *array = [NSArray arrayWithContentsOfFile:path];
        _foods = array;
    }
    return _foods;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self pickerView:self.pickView didSelectRow:0 inComponent:0];
    [self pickerView:self.pickView didSelectRow:0 inComponent:1];
    [self pickerView:self.pickView didSelectRow:0 inComponent:2];


    
}
#pragma mark - 数据源方法
//返回总共多少列;
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return self.foods.count;
}
//返回每列多收行;
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    NSArray *array =  self.foods[component];
    return array.count;
}
#pragma mark - 代理方法
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    NSArray *array = self.foods[component];
    return array[row];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
//    NSLog(@"component is %d,row is %d",component,row);
    NSArray *array = self.foods[component];
    NSString *str = array[row];
    switch (component) {
        case 0:
            self.fruitLable.text = str;
            break;
        case 1:
            self.mainfoodLable.text = str;
            break;
        case 2:
            self.drinkLable.text = str;
            break;
            default:
            break;
    }
}
#pragma mark - 随机点菜按钮
- (IBAction)randomPick {
//    找出总共多少列
    NSInteger countOfColumns = self.foods.count;
//    利用for循环找出每一列有多少行
    for (int i = 0; i < countOfColumns ; i ++) {
        NSArray *array = self.foods[i];
//        找出当前这一列有多少行;
        NSInteger countOfRows = array.count;
//        在这一列中随机产生一个数字;
        NSUInteger randomRow  = arc4random_uniform((int)countOfRows);
//        更改数据
        [self pickerView:self.pickView didSelectRow:randomRow inComponent:i];
        [self.pickView selectRow:randomRow inComponent:i animated:YES];
    }
}
@end

 

2016-1-3点菜系统demo的实现,pickerView的学习

标签:

原文地址:http://www.cnblogs.com/BJTUzhengli/p/5097279.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!