• iOS pickerView


    #import "ViewController.h"
    
    @interface ViewController ()<UIPickerViewDataSource,UIPickerViewDelegate>
    @property (nonatomic,strong)NSArray *foods;
    //显示水果
    @property (weak, nonatomic) IBOutlet UILabel *fruitLabel;
    //显示主食
    @property (weak, nonatomic) IBOutlet UILabel *mainLabel;
    //显示饮料
    @property (weak, nonatomic) IBOutlet UILabel *drinkLable;
    @property (weak, nonatomic) IBOutlet UIPickerView *pickerView;
    //随机
    - (IBAction)radom:(id)sender;
    //确认
    - (IBAction)certain:(id)sender;
    //取消
    - (IBAction)cancel:(id)sender;
    
    @end
    
    @implementation ViewController
    
    //懒加载数组
    -(NSArray *)foods{
        if (_foods==nil) {
            _foods=[NSArray arrayWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"foods" ofType:@"plist"]];
        }
        return _foods;
    }
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
        for (int i=0; i<3; i++) {
            [self pickerView:nil didSelectRow:0 inComponent:i];
        }
    }
    
    //总共多少列
    -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
        return self.foods.count;
    }
    
    //总共多少行
    -(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
        NSArray *subFood=self.foods[component];
        return subFood.count;
    }
    
    //第component列第row行显示文字内容
    -(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
        return self.foods[component][row];
    }
    
    //选中第component列第row行
    -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
        //选中行的文本显示对应的Label
        if(component==0){
            self.fruitLabel.text=self.foods[component][row];
        }else if(component==1){
            self.mainLabel.text=self.foods[component][row];
        }else if(component==2){
            self.drinkLable.text=self.foods[component][row];
        }
    }
    
    //设置行高
    -(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
        return 35;
    }
    
    //随机一份套餐
    - (IBAction)radom:(id)sender {
        for (int component=0;component<self.foods.count;component++){
            //第compnent列数组的总长度
            int count=(int)[self.foods[component]count];
            //旧的行号
            int oldRow=(int)[self.pickerView selectedRowInComponent:component];
            //第几行,默认旧行号与新行号相同
            int row=oldRow;
            //保证行号与上次不一样
            while(row==oldRow){
                row=arc4random()%count;
            }
            //让pickerView主动选中第compoent列的第row行
            [self.pickerView selectRow:row inComponent:component animated:YES];
            //设置label的文字
            [self pickerView:nil didSelectRow:row inComponent:component];
        }
    }
    
    - (IBAction)certain:(id)sender {
        //提示用户点餐成功
        UIAlertController *alertC=[UIAlertController alertControllerWithTitle:@"点餐成功" message:@"请稍后..." preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *certain=[UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:nil];
        [alertC addAction:certain];
        [self presentViewController:alertC animated:YES completion:nil];
    }
    
    - (IBAction)cancel:(id)sender {
        //直接初始化
        for(int i=0;i<3;i++){
            [self pickerView:nil didSelectRow:0 inComponent:i];
            [self.pickerView selectRow:0 inComponent:i animated:YES];
        }
    }
    @end
  • 相关阅读:
    MP3 PQF模块 matlab实现
    Warped Filter
    研究音频编解码要看什么书
    转:Firefox的未来版本可能备有内置视频聊天功能
    SILK,Speex,ISAC,AMRWB区别(1)
    SILK 的 Tilt的意思
    桓泽学音频编码
    整理下最近的学习思路(3月15日~4月9日)
    MPEG4 SSC备选标准SiCAS
    桓泽谈音乐(1)
  • 原文地址:https://www.cnblogs.com/hahayixiao/p/15759075.html
Copyright © 2020-2023  润新知