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

首頁 > 系統 > iOS > 正文

實例講解iOS應用開發中UIPickerView滾動選擇欄的用法

2019-10-21 18:55:07
字體:
來源:轉載
供稿:網友
這篇文章主要介紹了iOS應用開發中UIPickerView滾動選擇欄的用法,示例代碼基于傳統的Objective-C,需要的朋友可以參考下
 

基礎
1.UIPickerView 屬性

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

復制代碼代碼如下:

@property(nonatomic,assign) id dataSource;

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

@property(nonatomic,assign) id   delegate;

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

@property(nonatomic)   BOOL   showsSelectionIndicator;

    
一共有多少列
復制代碼代碼如下:

@property(nonatomic,readonly) NSInteger numberOfComponents;

 

2.UIPickerView方法

重新刷新所有列

復制代碼代碼如下:

- (void)reloadAllComponents;

 

重新刷新第component列

復制代碼代碼如下:

- (void)reloadComponent:(NSInteger)component;

 

主動選中第component列的第row行

復制代碼代碼如下:

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

 

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

復制代碼代碼如下:

- (NSInteger)selectedRowInComponent:(NSInteger)component;

 

3.UIPickerView數據源方法

一共有多少列

復制代碼代碼如下:

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

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

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

 

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

復制代碼代碼如下:

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

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

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

 

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

復制代碼代碼如下:

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

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

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

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

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

 

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

復制代碼代碼如下:

- (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];  
                   
                       
}  

然后,我們創建相關的代理方法
UIPickerViewDataSource 相關代理
復制代碼代碼如下:

#pragma Mark -- UIPickerViewDataSource  
// pickerView 列數  
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {  
    return 2;  
}  
  
// pickerView 每列個數  
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {  
    if (component == 0) {  
        return [_proTitleList count];  
    }  
      
    return [_proTimeList count];  
}  

UIPickerViewDelegate 相關代理方法
復制代碼代碼如下:

#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);  
    }  
      
}  
  
//返回當前行的內容,此處是將數組中數值添加到滾動的那個顯示欄上  
-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component  
{  
    if (component == 0) {  
        return [_proTitleList objectAtIndex:row];  
    } else {  
        return [_proTimeList objectAtIndex:row];  
          
    }  
}  

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

 

iOS應用開發,UIPickerView



注:相關教程知識閱讀請移步到IOS開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 中宁县| 溆浦县| 文成县| 锡林郭勒盟| 梅州市| 平谷区| 太白县| 衡水市| 龙泉市| 句容市| 油尖旺区| 泊头市| 宿州市| 商水县| 沿河| 海安县| 辽宁省| 海丰县| 八宿县| 花莲县| 含山县| 台山市| 吴忠市| 宁津县| 灵台县| 常宁市| 吉木萨尔县| 枝江市| 敦化市| 水富县| 黄骅市| 准格尔旗| 红安县| 忻州市| 封丘县| 德保县| 上栗县| 南雄市| 延庆县| 潞西市| 拜泉县|