UIPickerView 滚动选择器

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

/////别忘记 .h 中写代理  <UIPickerViewDelegate,UIPickerViewDataSource>
 
 
/////UIPickerView和UIDatePicker是类似的控件,只不过UIDatePicker是日期控件,只能放日期,而UIPickerView可以放任何东西。
 
- (void)viewDidLoad
{
    [super viewDidLoad];
    _dataArray = [[NSArray alloc] initWithObjects:@"a", @"b", @"c", @"d", @"e", @"f", nil];
     
    UIPickerView* pv = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 460-216, 320, 216)];
    pv.delegate = self;
    pv.dataSource = self;
    pv.showsSelectionIndicator = YES;
    [self.view addSubview:pv];
    //[pv release];
}
 
//多少组
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 2;
}
 
//每一组多少行
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if (component == 1) {
        return 3;
    }
    if (component == 0) {
        return _dataArray.count;
    }
    return 10;
}
 
//显示每一行的文本
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    if (component == 0) {
        return [_dataArray objectAtIndex:row];
    }
    return [NSString stringWithFormat:@"%d",row];
}
 
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    //如果是第0组
    if (component == 0) {
        //如果是第0组的第0行
        if (row == 0) {
            //return 显示的view
        }
        if (row == 1) {
            //
        }
    }
    UIView* myview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
    myview.backgroundColor = [UIColor redColor];
    return myview;
}
 
//选择一行就会调用这个方法
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    NSLog(@"选择的是第%d组,第%d行",component,row);
}