在餐厅里的点餐系统的核心控件就是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@interfaceViewController : 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>
@interfaceViewController () <UIPickerViewDataSource, UIPickerViewDelegate>{
NSArray*_foods;
}@end@implementationViewController- (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];intcount =_foods.count;for(inti =0; i < count; i++) {
[self pickerView:nil didSelectRow:0inComponent:i];
}
}
3.实现数据源方法用于展示数据#pragmamark - 数据源#pragmamark 有多少列
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{return_foods.count;
}#pragmamark 第component列有多少行
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
NSArray*array =_foods[component];returnarray.count;
}#pragmamark 每行显示什么内容、第component列第row行显示什么文字
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{return_foods[component][row];
}
4.实现代理方法,当选定时更新UI界面#pragmamark - 代理#pragmamark 选中了第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;
}elseif(component ==1) {//肉_meat.text =text;
}else{//饮料_water.text =text;
}
}
5.实现随机点餐功能#pragmamark 随机
- (IBAction)randomFood:(id)sender {//1.随机选中第0列的某一行(水果)//[self randomCompoment:0];//////2.随机选中第1列的某一行(肉)//[self randomCompoment:1];//////3.随机选中第2列的某一行(饮料)//[self randomCompoment:2];intcount =_foods.count;for(inti =0; i < count; i++) {
[self randomCompoment:i];
}
}#pragmamark 随机选中某一列的方法
- (void)randomCompoment:(int)component
{//0.获得第component列选中的行号intselectedRow =[_pickerView selectedRowInComponent:component];//1.随机生成行号intindex =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/
本文版权归作者和博客园共有,欢迎转载,但必须保留此段声明,且在文章页面明显位置给出原文连接。