(素材源碼)貓貓學IOS(二十)UI之UIPickerView_點菜系統

來源:互聯網
上載者:User

標籤:ui   ios   控制項   uipackervi   picker   

貓貓分享,必須精品

素材代碼地址:http://download.csdn.net/detail/u013357243/8596279
原創文章,歡迎轉載。轉載請註明:翟乃玉的部落格
地址:http://blog.csdn.net/u013357243?viewmode=contents

先看 ##

代碼
////  NJViewController.m//  01-點菜////  Created by apple on 14-6-3.//  Copyright (c) 2014年 heima. All rights reserved.//#import "NJViewController.h"@interface NJViewController ()<UIPickerViewDataSource, UIPickerViewDelegate>@property (weak, nonatomic) IBOutlet UIPickerView *pickerView;/** *  隨機按鈕點擊事件 */- (IBAction)randomFood:(UIButton *)sender;/** *  所有食物 */@property (nonatomic, strong) NSArray *foods;/** *  水果 */@property (weak, nonatomic) IBOutlet UILabel *fruitLabel;/** *  主菜 */@property (weak, nonatomic) IBOutlet UILabel *stapleLabel;/** *  飲料 */@property (weak, nonatomic) IBOutlet UILabel *drinkLabel;@end@implementation NJViewController- (void)viewDidLoad{    [super viewDidLoad];    // 設定預設選中的內容//    self.fruitLabel.text = self.foods[0][0];//    self.stapleLabel.text = self.foods[1][0];//    self.drinkLabel.text = self.foods[2][0];//    [self pickerView:nil didSelectRow:0 inComponent:0];//    [self pickerView:nil didSelectRow:0 inComponent:1];//    [self pickerView:nil didSelectRow:0 inComponent:2];    for (int component = 0; component < self.foods.count; component++) {        [self pickerView:nil didSelectRow:0 inComponent:component];    }}#pragma mark - UIPickerViewDataSource// 返回pickerView一共有多少列- (NSInteger) numberOfComponentsInPickerView:(UIPickerView *)pickerView{//    return 3;    return self.foods.count;}// 返回pickerView的第component列有多少行- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{//    return 4;    // 1.擷取對應列的數組    NSArray *subFoods = self.foods[component];    // 2.返回對應列的行數    return subFoods.count;}#pragma mark - UIPickerViewDelegate// 返回第component列的第row行顯示什麼內容- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{    // 1.擷取對應列的數組    NSArray *subFoods = self.foods[component];    // 2.擷取對應行的標題    NSString *name = subFoods[row];    return name;}// 當選中了pickerView的某一行的時候調用// 會將選中的列號和行號作為參數傳入// 只有通過手指選中某一行的時候才會調用- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{//    NSLog(@"component = %d, row = %d", component, row);    // 1.擷取對應列對應行的資料    NSString *name = self.foods[component][row];//    NSLog(@"name = %@", name);    // 2.判斷選擇的是哪一列, 根據列號設定對應的資料    if (0 == component) {        // 水果        self.fruitLabel.text = name;    }else if (1 == component)    {        // 主菜        self.stapleLabel.text = name;    }else    {        // 飲料        self.drinkLabel.text = name;    }}#pragma mark - 懶載入- (NSArray *)foods{    if (_foods == nil) {        NSString *fullPath = [[NSBundle mainBundle] pathForResource:@"foods.plist" ofType:nil];        _foods = [NSArray arrayWithContentsOfFile:fullPath];    }    return _foods;}#pragma mark - 監聽按鈕點擊- (IBAction)randomFood:(UIButton *)sender {    // 讓pickerView主動選中某一行    // 讓pickerView選中inComponent列的Row行//    [self.pickerView selectRow:1 inComponent:0 animated:YES];    /*    [self.pickerView selectRow: arc4random() % 12 inComponent:0 animated:YES];     [self.pickerView selectRow: arc4random() % 15 inComponent:1 animated:YES];     [self.pickerView selectRow: arc4random() % 10 inComponent:2 animated:YES];     *///    [self.foods objectAtIndex:0]; == self.foods[0];//    [self.foods[0] count];    /*    // 根據每一列的元素個數產生隨機值    [self.pickerView selectRow: arc4random() % [self.foods[0] count] inComponent:0 animated:YES];    [self.pickerView selectRow: arc4random() % [self.foods[1] count] inComponent:1 animated:YES];    [self.pickerView selectRow: arc4random() % [self.foods[2] count] inComponent:2 animated:YES];    */    for (int component = 0; component < self.foods.count; component++) {        // 擷取對應列的資料總數        int total = [self.foods[component] count];        // 根據每一列的總數產生隨機數(當前產生的隨機數)        int randomNumber = arc4random() % total;        // 擷取當前選中的行(上一次隨機後移動到的行)        int oldRow =  [self.pickerView selectedRowInComponent:0];//        NSLog(@"oldRow = %d", oldRow);        // 比較上一次的行號和當前產生的隨機數是否相同, 如果相同重建        while (oldRow == randomNumber) {            randomNumber = arc4random() % total;        }        // 讓pickerview滾動到某一行        [self.pickerView selectRow: randomNumber inComponent:component animated:YES];        // 通過代碼選中某一行        [self pickerView:nil didSelectRow:randomNumber inComponent:component];    }}@end

ps:建立iOS交流學習群:304570962 可以加貓貓QQ:1764541256 或則znycat 讓我們一起努力學習吧。
翟乃玉的部落格
地址:http://blog.csdn.net/u013357243?viewmode=contents

(素材源碼)貓貓學IOS(二十)UI之UIPickerView_點菜系統

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.