点餐系统思路
步骤1:遵照和实现UIPickerView的数据源。
步骤2:加载plist文件,把数据寄存在NSArray数组中。
- (NSArray *)foodArray
{
if (_foodArray ==
nil) {
_foodArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"foods.plist"
ofType:nil]];
for (int
i =
0; i <
self.foodArray.count; i++) {
[self
pickerView:nil
didSelectRow:0
inComponent:i];
}
}
return _foodArray;
}
步骤3:遵照和实现UIPickerView的代理方法,监听选中每行的方法。
#pragma mark -
实现代理方法
/**
1共有多少组
*/
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return
self.foodArray.count;
}
/**
1组有多少行
*/
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
NSArray *foods =
self.foodArray[component];
return foods.count;
}
/**
1行显示甚么内容
*/
- (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];
}else
if(component ==
1){
self.mainFoodLabel.text =
self.foodArray[component][row];
}else
if(component ==2){
self.drinkLabel.text =
self.foodArray[component][row];
}
}
步骤4:监听UIBarButtonItem随机的事件
- (IBAction)randomClickBtn
{
for (int
i =
0;i <
self.foodArray.count;i++) {
//
根据组算出有多少行
int cou = [self.foodArray[i] count];
//
获得被选中的
int oldRow = [self.picker selectedRowInComponent:i];
int row = oldRow;
//
如果上1个和下1个相等,就随机
while (row == oldRow) {
row = arc4random_uniform(cou);
}
[self.picker selectRow:row inComponent:i animated:YES];
[self
pickerView:nil
didSelectRow:row inComponent:i];
}
}