Xcode:UIPickerView更改单个行背景颜色

时间:2012-03-21 02:27:00

标签: xcode ios5 uipickerview background-color

我有一个带有3个组件(或列)的UIPickerView,每个列都有不同数量的行或项。我需要能够为每一行设置背景颜色。我做了一些挖掘,找到了几乎可以工作的东西,但不完全是我需要的东西:

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

CGRect imageFrame = CGRectMake(0.0, 0.0, 15, 15);
UIImageView *label = [[[UIImageView alloc] initWithFrame:imageFrame] **autorelease**];

if (row == 0)
{
    label.backgroundColor = [UIColor redColor];
}
if (row == 1)
{
    label.backgroundColor = [UIColor blueColor];
}
if (row == 2)
{
    label.backgroundColor = [UIColor blackColor];
}   
return label;
}

上面的代码将每个组件/列视为相同,并为每一行分配相同的颜色序列。例如:

第1列将具有:红色/蓝色/黑色 第2列将具有:红色/蓝色/黑色 第3列将具有:红色/蓝色/黑色

我需要能够为每列使用独特的颜色序列。我不需要每列具有与上述相同的颜色序列。例如,我可能会有以下内容:

第1列:红色/绿色/黑色 第2列:蓝色/紫色/红色/黄色 第3列:白色/红色

2 个答案:

答案 0 :(得分:0)

如果我正确理解你,你需要打开组件。例如:

- (UIView *) pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row 
           forComponent:(NSInteger)component reusingView:(UIView *)view {
    CGRect imageFrame = CGRectMake(0.0, 0.0, 15, 15);
    UIImageView *label = [[[UIImageView alloc] initWithFrame:imageFrame] **autorelease**];

    switch (component) {
        //I'm using enumeration types for the components because its pretty
        case THIRD_COMPONENT:
            if (row == 0)
            {
                 label.backgroundColor = [UIColor whiteColor];
            }
            if (row == 1)
            {
                 label.backgroundColor = [UIColor redColor];
            } 
            break;
        case SECOND_COMPONENT:
            if (row == 0)
            {
                label.backgroundColor = [UIColor blueColor];
            } 
            if (row == 1)
            {
                 label.backgroundColor = [UIColor purpleColor];
            }
            if (row == 2)
            {
                 label.backgroundColor = [UIColor redColor];
            }
            if (row == 3)
            {
                 label.backgroundColor = [UIColor yellow];
            }
            break;
        case FIRST_COMPONENT:
            if (row == 0)
            {
                label.backgroundColor = [UIColor redColor];
            } 
            if (row == 1)
            {
                 label.backgroundColor = [UIColor blueColor];
            }
            if (row == 2)
            {
                 label.backgroundColor = [UIColor blackColor];
            }
            break;
    }

    return label;
} 

它有点长而笨重,但是给它一个镜头,看看它是否解决了你的问题。我可能建议存储标签,其backgroundColor已经在每个组件的数组中设置。这样,而不是让所有那些if语句的新switch语句看起来像:

switch (component) {
    case THIRD_COMPONENT:
        label = [thirdBandColorsArray objectAtIndex:row];
        break;
    case SECOND_COMPONENT:
        label = [secondBandColorsArray objectAtIndex:row];
        break;
    case FIRST_COMPONENT:
        label = [firstBandColorsArray objectAtIndex:row];
        break;
}

我必须做一些与你在我的一个应用程序中描述的内容类似的东西,这就是我做到的。就像我说的那样,那只是一个建议。希望这会有所帮助。

答案 1 :(得分:0)

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    UILabel *label = (UILabel*) view;
    if (label == nil) {
        label = [[UILabel alloc] init];
    }

    label.textColor  = [UIColor whiteColor];
    label.backgroundColor = [UIColor setAnyRandomColor];
    CGSize rowSize = [pickerView rowSizeForComponent:component];
    CGRect labelRect = CGRectMake (0, 0, rowSize.width, rowSize.height);
    [label setFrame:labelRect];

    return label;
}