国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

iOS-UIPickerView餐廳點餐系統

2019-11-14 20:40:44
字體:
來源:轉載
供稿:網友

 

在餐廳里的點餐系統的核心控件就是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://m.survivalescaperooms.com/qingche/
本文版權歸作者和博客園共有,歡迎轉載,但必須保留此段聲明,且在文章頁面明顯位置給出原文連接。 

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 庆云县| 桦甸市| 疏勒县| 龙山县| 玛纳斯县| 柳州市| 泾源县| 建湖县| 湟中县| 偃师市| 玉溪市| 买车| 安吉县| 海口市| 简阳市| 来宾市| 建平县| 锡林郭勒盟| 共和县| 富源县| 闽侯县| 瓦房店市| 扎赉特旗| 河津市| 昌宁县| 林州市| 梁平县| 怀仁县| 永顺县| 修武县| 岳西县| 德保县| 泰来县| 辽阳市| 迁西县| 乡城县| 大新县| 绥滨县| 清远市| 安丘市| 蒲城县|