欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

實(shí)例講解iOS應(yīng)用開發(fā)中UIPickerView滾動(dòng)選擇欄的用法

 更新時(shí)間:2016年04月01日 09:18:28   作者:lwjok2007  
這篇文章主要介紹了iOS應(yīng)用開發(fā)中UIPickerView滾動(dòng)選擇欄的用法,示例代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下

基礎(chǔ)
1.UIPickerView 屬性

數(shù)據(jù)源(用來告訴UIPickerView有多少列多少行)

復(fù)制代碼 代碼如下:

@property(nonatomic,assign) id dataSource;

   
代理(用來告訴UIPickerView每1列的每1行顯示什么內(nèi)容,監(jiān)聽UIPickerView的選擇)
復(fù)制代碼 代碼如下:

@property(nonatomic,assign) id   delegate;

   
是否要顯示選中的指示器
復(fù)制代碼 代碼如下:

@property(nonatomic)   BOOL   showsSelectionIndicator;

   
一共有多少列
復(fù)制代碼 代碼如下:

@property(nonatomic,readonly) NSInteger numberOfComponents;

2.UIPickerView方法

重新刷新所有列

復(fù)制代碼 代碼如下:

- (void)reloadAllComponents;

重新刷新第component列

復(fù)制代碼 代碼如下:

- (void)reloadComponent:(NSInteger)component;

主動(dòng)選中第component列的第row行

復(fù)制代碼 代碼如下:

- (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated;

獲得第component列的當(dāng)前選中的行號(hào)

復(fù)制代碼 代碼如下:

- (NSInteger)selectedRowInComponent:(NSInteger)component;

3.UIPickerView數(shù)據(jù)源方法

一共有多少列

復(fù)制代碼 代碼如下:

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;

第component列一共有多少行
復(fù)制代碼 代碼如下:

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;

4.UIPickerView代理方法
第component列的寬度是多少

復(fù)制代碼 代碼如下:

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component;

第component列的行高是多少
復(fù)制代碼 代碼如下:

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component;

第component列第row行顯示什么文字

復(fù)制代碼 代碼如下:

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;

第component列第row行顯示怎樣的view(內(nèi)容)
復(fù)制代碼 代碼如下:

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view;

選中了pickerView的第component列第row行
復(fù)制代碼 代碼如下:

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component;

實(shí)例
UIPickerView 作為iOS的一個(gè)常用控件相信大家都有這方面的需求。
今天我們就簡單創(chuàng)建一個(gè):
新建項(xiàng)目 命名:TestUIPickerView
在默認(rèn)生成的ViewController中創(chuàng)建UIPickerView
首先在viewDidLoad 的方法中創(chuàng)建

復(fù)制代碼 代碼如下:

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
     
    // 選擇框 
    UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 100, 320, 216)]; 
    // 顯示選中框 
    pickerView.showsSelectionIndicator=YES; 
    pickerView.dataSource = self; 
    pickerView.delegate = self; 
    [self.view addSubview:pickerView]; 
     
    _proTimeList = [[NSArray alloc]initWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",nil]; 
    _proTitleList = [[NSArray alloc]initWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",nil]; 
                  
                      


然后,我們創(chuàng)建相關(guān)的代理方法
UIPickerViewDataSource 相關(guān)代理
復(fù)制代碼 代碼如下:

#pragma Mark -- UIPickerViewDataSource 
// pickerView 列數(shù) 
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView { 
    return 2; 

 
// pickerView 每列個(gè)數(shù) 
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { 
    if (component == 0) { 
        return [_proTitleList count]; 
    } 
     
    return [_proTimeList count]; 


UIPickerViewDelegate 相關(guān)代理方法
復(fù)制代碼 代碼如下:

#pragma Mark -- UIPickerViewDelegate 
// 每列寬度 
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component { 
     
    if (component == 1) { 
        return 40; 
    } 
    return 180; 

// 返回選中的行 
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component 

    if (component == 0) { 
        NSString  *_proNameStr = [_proTitleList objectAtIndex:row]; 
        NSLog(@"nameStr=%@",_proNameStr); 
    } else { 
        NSString  *_proTimeStr = [_proTimeList objectAtIndex:row]; 
        NSLog(@"_proTimeStr=%@",_proTimeStr); 
    } 
     

 
//返回當(dāng)前行的內(nèi)容,此處是將數(shù)組中數(shù)值添加到滾動(dòng)的那個(gè)顯示欄上 
-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component 

    if (component == 0) { 
        return [_proTitleList objectAtIndex:row]; 
    } else { 
        return [_proTimeList objectAtIndex:row]; 
         
    } 


完成以上代碼之后 我們就可以運(yùn)行項(xiàng)目查看效果
如下圖:

20164191743901.jpg (640×960)

相關(guān)文章

最新評(píng)論