ios中 pickerView的用法

來源:互聯網
上載者:User

標籤:plist   oftype   file   文字   nbu   picker   type   今天   blog   

今天是一個特殊的日子(Mac pro 敲的 爽。。。 昨天到的)

////  QRViewController.m//#import "QRViewController.h"@interface QRViewController ()<UIPickerViewDataSource,UIPickerViewDelegate>@property (weak, nonatomic) IBOutlet UILabel *fruitLable;//水果@property (weak, nonatomic) IBOutlet UILabel *mainLabel;//主菜@property (weak, nonatomic) IBOutlet UILabel *drinkLabel;//飲料@property (nonatomic,strong) NSArray *foods;@property (weak, nonatomic) IBOutlet UIPickerView *pickerView;- (IBAction)ranDom;@end@implementation QRViewController- (void)viewDidLoad {    [super viewDidLoad];    for (int i=0; i<self.foods.count; i++) {        [self pickerView:nil didSelectRow:0 inComponent:i];    }    //[self pickerView:nil didSelectRow:0 inComponent:0];    //[self pickerView:nil didSelectRow:0 inComponent:1];    //[self pickerView:nil didSelectRow:0 inComponent:2];}/** *懶載入資料 */- (NSArray *)foods{    if(_foods==nil){        NSString *path=[[NSBundle mainBundle] pathForResource:@"foods" ofType:@"plist"];        NSArray *arraylist=[NSArray arrayWithContentsOfFile:path];        _foods=arraylist;    }    return _foods;}#pragma mark   -資料來源方法/** *一共多少列 */- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{    return self.foods.count;}/** * 某一列顯示多少行 */- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{    NSArray *subfoods=self.foods[component];    return subfoods.count;}#pragma mark - 代理方法/** *某一列中的某一行顯示的資料 */- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{    return self.foods[component][row];}/** *選中某一行一列 */- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{    if(component==0){//水果        self.fruitLable.text=self.foods[component][row];    }else if(component==1){//主菜        self.mainLabel.text=self.foods[component][row];    }else if(component==2){//飲料        self.drinkLabel.text=self.foods[component][row];    }}/** *隨機產生一個 */- (IBAction)ranDom {    for (int i=0; i<self.foods.count; i++) {        //每一行的總長度        int count=[self.foods[i] count];        //產生一個隨機數        int row=arc4random()%count;        //設定pickerView選中的列的行        [self.pickerView selectRow:row inComponent:i animated:YES];        //設定Label的文字        [self pickerView:nil didSelectRow:row inComponent:i];    }    }@end

 

ios中 pickerView的用法

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.