码迷,mamicode.com
首页 > 移动开发 > 详细

(素材源码)猫猫学IOS(二十)UI之UIPickerView_点菜系统

时间:2015-04-15 13:37:39      阅读:319      评论:0      收藏:0      [点我收藏+]

标签:ui   ios   控件   uipackervi   picker   

猫猫分享,必须精品

素材代码地址:http://download.csdn.net/detail/u013357243/8596279
原创文章,欢迎转载。转载请注明:翟乃玉的博客
地址:http://blog.csdn.net/u013357243?viewmode=contents

先看效果图 ##

技术分享

代码

//
//  NJViewController.m
//  01-点菜
//
//  Created by apple on 14-6-3.
//  Copyright (c) 2014年 heima. All rights reserved.
//

#import "NJViewController.h"

@interface NJViewController ()<UIPickerViewDataSource, UIPickerViewDelegate>
@property (weak, nonatomic) IBOutlet UIPickerView *pickerView;
/**
 *  随机按钮点击事件
 */
- (IBAction)randomFood:(UIButton *)sender;
/**
 *  所有食物
 */
@property (nonatomic, strong) NSArray *foods;
/**
 *  水果
 */
@property (weak, nonatomic) IBOutlet UILabel *fruitLabel;
/**
 *  主菜
 */
@property (weak, nonatomic) IBOutlet UILabel *stapleLabel;
/**
 *  饮料
 */
@property (weak, nonatomic) IBOutlet UILabel *drinkLabel;


@end

@implementation NJViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // 设置默认选中的内容
//    self.fruitLabel.text = self.foods[0][0];
//    self.stapleLabel.text = self.foods[1][0];
//    self.drinkLabel.text = self.foods[2][0];

//    [self pickerView:nil didSelectRow:0 inComponent:0];
//    [self pickerView:nil didSelectRow:0 inComponent:1];
//    [self pickerView:nil didSelectRow:0 inComponent:2];

    for (int component = 0; component < self.foods.count; component++) {
        [self pickerView:nil didSelectRow:0 inComponent:component];
    }
}

#pragma mark - UIPickerViewDataSource
// 返回pickerView一共有多少列
- (NSInteger) numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
//    return 3;
    return self.foods.count;
}

// 返回pickerView的第component列有多少行
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
//    return 4;
    // 1.获取对应列的数组
    NSArray *subFoods = self.foods[component];
    // 2.返回对应列的行数
    return subFoods.count;
}

#pragma mark - UIPickerViewDelegate
// 返回第component列的第row行显示什么内容
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    // 1.获取对应列的数组
    NSArray *subFoods = self.foods[component];
    // 2.获取对应行的标题
    NSString *name = subFoods[row];
    return name;
}

// 当选中了pickerView的某一行的时候调用
// 会将选中的列号和行号作为参数传入
// 只有通过手指选中某一行的时候才会调用
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
//    NSLog(@"component = %d, row = %d", component, row);
    // 1.获取对应列对应行的数据
    NSString *name = self.foods[component][row];
//    NSLog(@"name = %@", name);

    // 2.判断选择的是哪一列, 根据列号设置对应的数据
    if (0 == component) {
        // 水果
        self.fruitLabel.text = name;
    }else if (1 == component)
    {
        // 主菜
        self.stapleLabel.text = name;
    }else
    {
        // 饮料
        self.drinkLabel.text = name;
    }
}

#pragma mark - 懒加载
- (NSArray *)foods
{

    if (_foods == nil) {
        NSString *fullPath = [[NSBundle mainBundle] pathForResource:@"foods.plist" ofType:nil];
        _foods = [NSArray arrayWithContentsOfFile:fullPath];
    }
    return _foods;
}
#pragma mark - 监听按钮点击
- (IBAction)randomFood:(UIButton *)sender {
    // 让pickerView主动选中某一行
    // 让pickerView选中inComponent列的Row行
//    [self.pickerView selectRow:1 inComponent:0 animated:YES];

    /*
    [self.pickerView selectRow: arc4random() % 12 inComponent:0 animated:YES];
     [self.pickerView selectRow: arc4random() % 15 inComponent:1 animated:YES];
     [self.pickerView selectRow: arc4random() % 10 inComponent:2 animated:YES];
     */

//    [self.foods objectAtIndex:0]; == self.foods[0];
//    [self.foods[0] count];

    /*
    // 根据每一列的元素个数生成随机值
    [self.pickerView selectRow: arc4random() % [self.foods[0] count] inComponent:0 animated:YES];
    [self.pickerView selectRow: arc4random() % [self.foods[1] count] inComponent:1 animated:YES];
    [self.pickerView selectRow: arc4random() % [self.foods[2] count] inComponent:2 animated:YES];
    */

    for (int component = 0; component < self.foods.count; component++) {
        // 获取对应列的数据总数
        int total = [self.foods[component] count];
        // 根据每一列的总数生成随机数(当前生成的随机数)
        int randomNumber = arc4random() % total;

        // 获取当前选中的行(上一次随机后移动到的行)
        int oldRow =  [self.pickerView selectedRowInComponent:0];
//        NSLog(@"oldRow = %d", oldRow);

        // 比较上一次的行号和当前生成的随机数是否相同, 如果相同重新生成
        while (oldRow == randomNumber) {
            randomNumber = arc4random() % total;
        }

        // 让pickerview滚动到某一行
        [self.pickerView selectRow: randomNumber inComponent:component animated:YES];

        // 通过代码选中某一行
        [self pickerView:nil didSelectRow:randomNumber inComponent:component];
    }
}
@end

ps:新建iOS交流学习群:304570962 可以加猫猫QQ:1764541256 或则微信znycat 让我们一起努力学习吧。
翟乃玉的博客
地址:http://blog.csdn.net/u013357243?viewmode=contents

(素材源码)猫猫学IOS(二十)UI之UIPickerView_点菜系统

标签:ui   ios   控件   uipackervi   picker   

原文地址:http://blog.csdn.net/u013357243/article/details/45057267

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