PickerView中的第一个组件仅在自定义上发生更改

时间:2013-01-03 00:48:22

标签: iphone objective-c ios

我正在尝试自定义UIPickerView,它主要工作。但是,设置背景颜色仅适用于第一个组件(即第一列)。我似乎无法影响其他两列。我认为问题可能在于PickerView中组件的加载顺序,但我不确定。非常感谢任何帮助。

#pragma mark PickerView DataSource

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    //[(UIView*)[[_picker subviews] objectAtIndex:0] setAlpha:0.1f];
    return 3;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{

        [self setColors];

    NSLog(@"number of components %d",[pickerView numberOfComponents]);
        if(component == 0)

    {
        //use Array1
        [self setColors];
        return [_pickerArray count];


    }

    else if (component==1)

    {
        [self setColors];
        NSLog(@"component 1");
        return [_pickerArray count];

    } else if (component==2) {
        NSLog(@"component 2");
        [self setColors];
        return [_pickerArray count];
    } else {

        return 3;
    }


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

        if(component == 0)

        {

            //use Array1
            return [_pickerArray objectAtIndex:row];


        }

        else 

        {

            //use Array2
            return [_pickerArrayTwo objectAtIndex:row];

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

    [self setColors];
    UILabel *label= [[UILabel alloc] initWithFrame:CGRectMake(0.0, -130.0, 60.0, 60.0)];
    [label setBackgroundColor:[UIColor redColor]];
    [label setTextColor:[UIColor blueColor]];
    [label setFont:[UIFont boldSystemFontOfSize:40.0]];
    [label setText:[NSString stringWithFormat:@"%d",row]];
    label.backgroundColor = [UIColor clearColor];
    return label;
}
-(void)setColors
{
    [(UIView*)[[_picker  subviews] objectAtIndex:0] setAlpha:0.0f];

    [(UIView*)[[_picker subviews] objectAtIndex:1] setAlpha:0.0f];
    [(UIView*)[[_picker subviews] objectAtIndex:2] setAlpha:0.0f];
    [(UIView*)[[_picker subviews] objectAtIndex:3] setAlpha:0.0f];
    // [(UIView*)[[_picker subviews] objectAtIndex:4] setAlpha:0.0f];
    [(UIView*)[[_picker subviews] objectAtIndex:0] setHidden:YES];

    // [(UIView*)[[_picker  subviews] objectAtIndex:2] setAlpha:0.5f];
    // NSLog(@"%@",[_picker subviews]);
    _picker.backgroundColor = [UIColor clearColor];

}

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

确定。我也尝试过自定义UIPickerView但是没有成功。相反,我最终得到了一个自定义的pickerview AFPickerView,可以在https://github.com/arkichek/AFPickerView找到?

它是一个出色的自定义选择器库。