iOS7_關於UIPickerView_如何?UIPickerView_如何設定UIPickerView組數_每組資料的行數

來源:互聯網
上載者:User

標籤:style   blog   http   color   os   io   strong   for   檔案   

在介面上實現一個UIPickerView:

1、建立一個 singleViewApplication,在 xxxViewController.m檔案中:

 1 @interface moboViewController ()

2 @property (nonatomic, strong) UIPickerView *picker;

3 @end 

2、在 -(void)viewDidLoad方法中,實現:

- (void)viewDidLoad{    [super viewDidLoad];        //執行個體化    self.picker = [[UIPickerView alloc]init];        //位置    self.picker.center = self.view.center;        //添加到 view    [self.view addSubview:self.picker];}

3、CMD+R 跑起來:

發現是空白!為什麼呢?我們進UIPickerView的標頭檔看一看,按住CMD點擊UIPickerView,我們發現:

 1 @protocol UIPickerViewDataSource, UIPickerViewDelegate; 

哈哈!是因為我們還沒有遵守TA的 資料來源協議和代理方法 呢!

4、那我們遵守 資料來源和 代理協議,在 controller.m檔案中的 @interface 部分後面,來遵守協議:

 1 @interface moboViewController ()<UIPickerViewDelegate,UIPickerViewDataSource> //遵守協議

2 @property (nonatomic, strong) UIPickerView *picker;  

3 @end 

5、在 -(void)viewDidLoad 中我們設定 代理 和資料來源為 self:

 1 - (void)viewDidLoad 2 { 3     [super viewDidLoad]; 4      5     //執行個體化 6     self.picker = [[UIPickerView alloc]init]; 7      8     //設定代理 9     self.picker.delegate = self;10     11     //設定資料來源12     self.picker.dataSource = self;13     14     //位置15     self.picker.center = self.view.center;16     17     //添加到 view18     [self.view addSubview:self.picker];19 20 }

6、實現資料來源和代理方法:

//返回組數,我們直接來返回2組好了。

1 - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView2 {3     if ([pickerView isEqual:self.picker]) { //如果傳進來的pickerview是剛實現的picker就返回24         return 2;5     }6     7     return 0; //如果不是就返回08 }

//返回每組的資料的行數,10行好了。

1 -(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component2 {3     if ([pickerView isEqual:self.picker]) { //如果傳進來的pickerview是剛實現的picker就返回104         return 10;5     }6     7     return 0;//如果不是就返回08 }
 1 /* 2  實現代理方法 3  */ 4  5 - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component 6 { 7     if ([pickerView isEqual:self.picker]) { 8          9         if (component == 0) {10             return [NSString stringWithFormat:@"A %d", row+1];11         }12         else if(component ==1){13             return [NSString stringWithFormat:@"B %d", row+1];14         }15         16     }17     return  0;18 }

7、CMD+R,跑起來!

 

iOS7_關於UIPickerView_如何?UIPickerView_如何設定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.