·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> app软件开发 >> IOS开发 >> iOS-UIPickerView餐厅点餐系统

iOS-UIPickerView餐厅点餐系统

作者:佚名      IOS开发编辑:admin      更新时间:2022-07-23

 

在餐厅里的点餐系统的核心控件就是UipickerView

今天晚上在整理以前的项目笔记时,特意把UIPickerView单独拿出来,做了一个简陋的点餐道具。

因为没有素材图片,所有大家将就看看吧

 

 

 

0.用到的主要方法  

- 数据源方法

有多少列 - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView { return _foods.count; } 第component列有多少行 - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { NSArray *array = _foods[component]; return array.count; } 每行显示什么内容、第component列第row行显示什么文字 - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { return _foods[component][row]; } - 代理方法
选中了第component列第row行就会调用 // 只有手动选中了某一行才会通知代理 - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { 2.选中某一行
[_pickerView selectRow:index inComponent:component animated:YES];

 

1.UI界面搭建,将需要用到的控件拖入头文件  

UIPickerView

@interface ViewController : UIViewController
@PRoperty (weak, nonatomic) IBOutlet UIPickerView *pickerView;
@property (weak, nonatomic) IBOutlet UILabel *fruit;
@property (weak, nonatomic) IBOutlet UILabel *meat;
@property (weak, nonatomic) IBOutlet UILabel *water;
- (IBAction)randomFood:(id)sender;

@end

 

2.初始化数据  

记得实现数据源和代理

<UIPickerViewDataSource, UIPickerViewDelegate>

 

@interface ViewController () <UIPickerViewDataSource, UIPickerViewDelegate>
{
    NSArray *_foods;
}
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // 1.加载数据
    _foods = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"foods.plist" ofType:nil]];
    
    // 2.设置默认值
//    _fruit.text = _foods[0][0];
//    _meat.text = _foods[1][0];
//    _water.text = _foods[2][0];
    int count = _foods.count;
    for (int i = 0; i < count; i++) {
        [self pickerView:nil didSelectRow:0 inComponent:i];
    }
}

 

3.实现数据源方法用于展示数据  

#pragma mark - 数据源
#pragma mark 有多少列
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return _foods.count;
}

#pragma mark 第component列有多少行
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    NSArray *array = _foods[component];
    
    return array.count;
}

#pragma mark 每行显示什么内容、第component列第row行显示什么文字
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return _foods[component][row];
}

 

4.实现代理方法,当选定时更新UI界面  

#pragma mark - 代理
#pragma mark 选中了第component列第row行就会调用
// 只有手动选中了某一行才会通知代理
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    // 1.取出选中那行的文字
    NSString *text = _foods[component][row];
    
    // 2.显示到对应的label上面
    if (component == 0) { // 水果
        _fruit.text = text;
    } else if (component == 1) { // 肉
        _meat.text = text;
    } else { // 饮料
        _water.text = text;
    }
}

 

5.实现随机点餐功能  

#pragma mark 随机
- (IBAction)randomFood:(id)sender {
    // 1.随机选中第0列的某一行(水果)
//    [self randomCompoment:0];
//    
//    // 2.随机选中第1列的某一行(肉)
//    [self randomCompoment:1];
//    
//    // 3.随机选中第2列的某一行(饮料)
//    [self randomCompoment:2];
    
    int count = _foods.count;
    for (int i = 0; i < count; i++) {
        [self randomCompoment:i];
    }
}

#pragma mark 随机选中某一列的方法
- (void)randomCompoment:(int)component
{
    // 0.获得第component列选中的行号
    int selectedRow = [_pickerView selectedRowInComponent:component];
    
    // 1.随机生成行号
    int index = selectedRow;
    while (index == selectedRow) {
        index = arc4random_uniform([_foods[component] count]);
    } // 一定会生成不一样的行号
    
    // 2.选中某一行
    [_pickerView selectRow:index inComponent:component animated:YES];
    
    // 3.更改文字
    [self pickerView:nil didSelectRow:index inComponent:component];
}

 

 

作者: 清澈Saup
出处: http://www.cnblogs.com/qingche/
本文版权归作者和博客园共有,欢迎转载,但必须保留此段声明,且在文章页面明显位置给出原文连接。