首页 > 代码库 > 点餐系统思路

点餐系统思路

点餐系统思路

步骤一:遵守和实现UIPickerView的数据源。

步骤二:加载plist文件,把数据存放在NSArray数组中。
- (NSArray *)foodArray
{
   
if (_foodArray ==nil) {
        _foodArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:
@"foods.plist"ofType:nil]];
       
for (inti = 0; i < self.foodArray.count; i++) {
            [
selfpickerView:nildidSelectRow:0inComponent:i];
        }
    }
   
return _foodArray;
}

步骤三:遵守和实现UIPickerView的代理方法,监听选中每一行的方法。
#pragma mark -实现代理方法

/**一共有多少组*/
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
   
return self.foodArray.count;
}

/**一组有多少行*/
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    NSArray *foods =
self.foodArray[component];
   
return foods.count;
}

/**一行显示什么内容*/
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
   
return  self.foodArray[component][row];
}

/**选中*/
- (
void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
   
if (component ==0) {
       
self.fruitLabel.text =self.foodArray[component][row];
    }
elseif(component ==1){
       
self.mainFoodLabel.text =self.foodArray[component][row];
    }
elseif(component ==2){
       
self.drinkLabel.text =self.foodArray[component][row];
    }
}

步骤四:监听UIBarButtonItem随机的事件
- (
IBAction)randomClickBtn
{
   
for (inti = 0;i < self.foodArray.count;i++) {
       
// 根据组算出有多少行
       
int cou = [self.foodArray[i] count];
       
// 取得被选中的
       
int oldRow = [self.picker selectedRowInComponent:i];
       
int row = oldRow;
       
// 如果上一个和下一个相等,就随机
       
while (row == oldRow) {
            row = arc4random_uniform(cou);
        }
       
        [
self.picker selectRow:row inComponent:i animated:YES];
        [
selfpickerView:nildidSelectRow:row inComponent:i];
    }
}

点餐系统思路