Xcode:UIPickerView更改单个行的背景色 [英] Xcode: UIPickerView Change Individual Row Background Color

查看:330
本文介绍了Xcode:UIPickerView更改单个行的背景色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含3个组件(或列)的UIPickerView,并且每列具有不同数量的行或项目.我需要能够为每一行设置背景色.我做了一些挖掘,发现几乎可以使用的东西,但并非完全符合我的需要:

I have a UIPickerView with 3 components (or columns), and each column has a different amount of rows or items. I need to be able to set the background color for each row. I did some digging and found something that almost works, but not exactly what I need:

- (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;
}

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

The code above treats every component/column the same, and will assign the same color sequence to each row. So for example:

第1栏将包含:红色/蓝色/黑色 第2栏将包含:红色/蓝色/黑色 第3列将为:红色/蓝色/黑色

Column 1 will have: red/blue/black Column 2 will have: red/blue/black Column 3 will have: red/blue/black

我需要能够为每列使用唯一的颜色序列.我不需要每一列都具有如上所述的相同颜色顺序.因此,例如,我可能会有类似以下内容的东西:

I need to be able to use unique color sequences for each column. I do not need each column to have the same sequence of colors as mentioned above. So for example I may have something like the following:

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

Column 1: red/green/black Column 2: blue/purple/red/yellow Column 3: white/red

推荐答案

如果我对您的理解正确,则需要打开组件.例如:

If I'm understanding you correctly, you need to switch on component. For example:

- (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语句将类似于:

Its a little long and unwieldy but give that a shot and see if it fixes your problem. I might suggest storing the labels with their backgroundColor already set in an array for each component. That way instead of having all of those if statements your new switch statement would look like:

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;
}

我必须执行与您在其中一个应用程序中描述的内容类似的操作,而这正是我的操作方式.就像我说的那样,那只是一个建议.希望这会有所帮助.

I had to do something similar to what you're describing in one of my apps and that was how I did it. Like I said though, thats just a suggestion. Hope this helps.

这篇关于Xcode:UIPickerView更改单个行的背景色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆