zoukankan      html  css  js  c++  java
  • (素材源码)猫猫学IOS(二十)UI之UIPickerView_点菜系统

    猫猫分享,必须精品

    素材代码地址: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

  • 相关阅读:
    python爬虫面试总结
    Android 开发之避免被第三方使用代理抓包
    类的惰性属性
    [转载]Python: 你不知道的 super
    转 白话解析:一致性哈希算法 consistent hashing
    转 appium解决每次运行都需要安装Unlock以及AppiumSetting的问题
    233
    windows中Appium-desktop配合夜神模拟器的使用
    CentOS 6.4 添加永久静态路由所有方法汇总(原创)
    牛逼的lsof命令!!!
  • 原文地址:https://www.cnblogs.com/znycat/p/4521035.html
Copyright © 2011-2022 走看看