
在餐廳里的點餐系統的核心控件就是UipickerView
今天晚上在整理以前的項目筆記時,特意把UIPickerView單獨拿出來,做了一個簡陋的點餐道具。
因為沒有素材圖片,所有大家將就看看吧
- 數據源方法
有多少列- (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];
@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
記得實現數據源和代理
<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]; }}
#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];}
#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; }}
#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://m.survivalescaperooms.com/qingche/
本文版權歸作者和博客園共有,歡迎轉載,但必須保留此段聲明,且在文章頁面明顯位置給出原文連接。 
新聞熱點
疑難解答