国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > iOS > 正文

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

2020-07-26 03:23:39
字體:
供稿:網(wǎng)友

基礎(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列的當(dāng)前選中的行號

復(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
在默認(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 每列個數(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ù)值添加到滾動的那個顯示欄上 
-(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)

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 柳林县| 浦东新区| 潍坊市| 南和县| 元江| 融水| 友谊县| 调兵山市| 收藏| 胶南市| 苍南县| 施甸县| 天峻县| 昭觉县| 长宁县| 岱山县| 彩票| 宣威市| 兰考县| 安吉县| 黑水县| 永泰县| 怀化市| 精河县| 博野县| 柘荣县| 南丰县| 栖霞市| 荣成市| 东兴市| 南漳县| 宿松县| 介休市| 凌云县| 铁岭市| 社旗县| 湖北省| 新田县| 株洲市| 盐城市| 靖江市|