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

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

 更新時間:2016年04月01日 09:18:28   作者:lwjok2007  
這篇文章主要介紹了iOS應(yīng)用開發(fā)中UIPickerView滾動選擇欄的用法,示例代碼基于傳統(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;

主動選中第component列的第row行

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

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

獲得第component列的當前選中的行號

復(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;

實例
UIPickerView 作為iOS的一個常用控件相信大家都有這方面的需求。
今天我們就簡單創(chuàng)建一個:
新建項目 命名:TestUIPickerView
在默認生成的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 每列個數(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); 
    } 
     

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

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


完成以上代碼之后 我們就可以運行項目查看效果
如下圖:

20164191743901.jpg (640×960)

相關(guān)文章

  • iOS實現(xiàn)后臺長時間運行

    iOS實現(xiàn)后臺長時間運行

    這篇文章主要為大家詳細介紹了iOS實現(xiàn)后臺長時間運行,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-10-10
  • 基于IOS端微信分享失效的踩坑及解決方法

    基于IOS端微信分享失效的踩坑及解決方法

    下面小編就為大家分享一篇基于IOS端微信分享失效的踩坑及解決方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-01-01
  • AVFoundation AVCaptureSession媒體捕捉

    AVFoundation AVCaptureSession媒體捕捉

    這篇文章主要為大家介紹了ios開發(fā)AVFoundation AVCaptureSession媒體捕捉詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-10-10
  • IOS property屬性詳細介紹使用注意事項

    IOS property屬性詳細介紹使用注意事項

    這篇文章主要介紹了IOS property屬性詳細介紹使用注意事項的相關(guān)資料,需要的朋友可以參考下
    2017-02-02
  • iOS編寫下拉刷新控件

    iOS編寫下拉刷新控件

    這篇文章主要介紹了iOS編寫下拉刷新控件的相關(guān)資料,iOS如何寫個普通的下拉刷新的控件,需要了解的朋友可以參考下文
    2016-04-04
  • iOS利用UIScrollView實現(xiàn)圖片的縮放實例代碼

    iOS利用UIScrollView實現(xiàn)圖片的縮放實例代碼

    本篇文章主要介紹了iOS利用UIScrollView實現(xiàn)圖片的縮放實例代碼,具有一定的參考價值,有興趣的可以了解一下
    2017-07-07
  • iOS開發(fā)xconfig和script腳本使用詳解

    iOS開發(fā)xconfig和script腳本使用詳解

    這篇文章主要為大家介紹了iOS開發(fā)xconfig和script腳本使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-09-09
  • 寫給iOS程序員的命令行使用秘籍

    寫給iOS程序員的命令行使用秘籍

    寫給iOS程序員的命令行使用秘籍,多事情在命令行下處理會事半功倍,所以我就iOS程序員可能會用到的功能講述一下,感興趣的小伙伴們可以參考一下
    2016-03-03
  • iOS 封裝導(dǎo)航欄及返回,獲取控件所在控制器的實例

    iOS 封裝導(dǎo)航欄及返回,獲取控件所在控制器的實例

    下面小編就為大家分享一篇iOS 封裝導(dǎo)航欄及返回,獲取控件所在控制器的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-01-01
  • iOS UIBezierPath實現(xiàn)餅狀圖

    iOS UIBezierPath實現(xiàn)餅狀圖

    這篇文章主要為大家詳細介紹了iOS UIBezierPath實現(xiàn)餅狀圖,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-03-03

最新評論