实例讲解iOS应用开发中UIPickerView滚动选择栏的用法

2020-01-14 22:13:59刘景俊

 

第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 列数