UIPickerView

遵守
遵守
//初始化

  • (UIPickerView *)pickerView
    {
    if (_pickerView == nil) {
    //初始化一个pickerView
    _pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)];
    //设置背景色
    //设置代理
    _pickerView.delegate = self;
    _pickerView.dataSource = self;
    //默认显示当前的下标
    // [_pickerView selectRow:3 inComponent:0 animated:NO];
    }

    return _pickerView;
    }
    //返回文字

  • (NSString *)getCurrentSelectedInfo
    {

return string;

}

  • (instancetype)initWithFrame:(CGRect)frame
    {
    if (self = [super initWithFrame:frame]) {
    [self setupUI];
    }
    return self;
    }

  • (void)setupUI
    {
    // 加载数据

    arrayData = [NSMutableArray arrayWithObjects:@"请选择性别",@"男",@"女", nil];
    [self addSubview: self.pickerView];
    }

//返回有几列
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
//几行
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{

return arrayData.count;

}
//返回指定列,行的高度,就是自定义行的高度

  • (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    return 40.0f;
    }
    //返回指定列的宽度

  • (CGFloat) pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
    if (component==0) {//iOS6边框占10+10
    return self.frame.size.width/2;
    } else if(component==1){
    return self.frame.size.width/2;
    }
    return self.frame.size.width/2;
    }
    //显示的标题

  • (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{

    return [arrayData objectAtIndex:row];
    }
    //设置显示的Laber

  • (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
    {
    //设置分割线的颜色
    for(UIView *singleLine in pickerView.subviews)
    {
    if (singleLine.frame.size.height < 1)
    {
    singleLine.backgroundColor = colorText;
    }
    }
    UILabel *genderLabel = [UILabel new];
    genderLabel.textAlignment = NSTextAlignmentCenter;
    genderLabel.text = arrayData[row];

    genderLabel.adjustsFontSizeToFitWidth = YES;
    if (selectedRow == row) {
    genderLabel.textColor = colorButton;
    }else{
    genderLabel.textColor = colorText;
    }

    return genderLabel;
    }
    //被选择的行
    -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    selectedRow = row;
    [self.pickerView reloadAllComponents];//重点
    string =[arrayData objectAtIndex:row];
    }

你可能感兴趣的:(UIPickerView)