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

    猫猫分享,必须精品

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

    先看效果图 ##

    这里写图片描述

    UIPickerView控件

    UIPickerView用处:

    用来展示很多行(row) 很多列(component )的数据,多用于电子商务的点菜,城市选择等等。

    UIPickerView用法:

    他用起来跟tableView差不多,用法:

    1:设置代理和数据源

    @interface NYViewController ()<UIPickerViewDataSource, UIPickerViewDelegate>

    数据源:UIPickerViewDataSource,

    1,返回有多少列
    2,返回有多少行

    #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;
    }

    代理UIPickerViewDelegate

    返回第component列的第row行显示什么内容

    #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;
    }
    

    怎么监听选中哪一行

    didSelectRow
    当我们选中某一列某一行的时候, 我们就把相应的数据设置。

    // 当选中了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;
        }
    }
    

    实现随机事件

    如何让pickerView自己滚动到哪一行
    selectRow inComponent让pickerView主动的滚动到某一列某一行

    - (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];
        }
    }

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

  • 相关阅读:
    查看python关键字
    命令终端执行python
    Codeforces-462C. A Twisty Movement
    Codeforces-462A. A Compatible Pair
    Codeforces-446C. Pride
    Codeforces-Hello 2018C. Party Lemonade(贪心)
    Codeforces-33C. Wonderful Randomized Sum
    Codeforces-118D. Caesar's Legions(lazy dynamics)
    codeforces-73C. LionAge II
    Gym 101510C-Computer Science
  • 原文地址:https://www.cnblogs.com/znycat/p/4521036.html
Copyright © 2011-2022 走看看