0

I know i am going to ask a silly question but please provide some answer.

If i have table view, I want to set background color of table view and i have a string named color which have the value as "gray Color".

How to set background Color of table view with the help of string color.

For clarifications, Please get back to me.

Thanks.

4

3 回答 3

1

这里有一些你可以用来开始的代码。我使用了所有的UIColor's类颜色来保持代码简洁明了。如果您需要确定其他自定义颜色名称,例如您要求的“灰色颜色”,您将被迫为您要处理的每种颜色编写一个长 if 子句和 if 语句。

- (void)viewDidLoad
{
    [super viewDidLoad];

    // statically add UIColor class names (for sake of simplicity)
    self.colors = [NSArray arrayWithObjects:@"blackColor", 
                                            @"darkGrayColor",
                                            @"lightGrayColor",
                                            @"whiteColor",
                                            @"grayColor",
                                            @"redColor",
                                            @"greenColor",
                                            @"blueColor",
                                            @"cyanColor",
                                            @"yellowColor",
                                            @"magentaColor",
                                            @"orangeColor",
                                            @"purpleColor",
                                            @"brownColor",
                                            @"aColor",
                                            @"lightTextColor",
                                            @"darkTextColor",
                                            @"groupTableViewBackgroundColor",
                                            @"viewFlipsideBackgroundColor",
                                            @"scrollViewTexturedBackgroundColor",
                                            @"underPageBackgroundColor",
                                            nil];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (!cell)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    NSString *colorName = [self.colors objectAtIndex:indexPath.row];
    SEL colorSelector  = NSSelectorFromString(colorName);
    if ([UIColor respondsToSelector:colorSelector])
    {
        UIColor *cellColor = [UIColor performSelector:colorSelector];
        const CGFloat *componentColors = CGColorGetComponents(cellColor.CGColor);
        int numComponents = CGColorGetNumberOfComponents(cellColor.CGColor);

        // get inverse color for cell text 
        UIColor *inverseColor = [UIColor whiteColor];
        if (numComponents == 4)
        {
            inverseColor = [[[UIColor alloc] initWithRed:(255 - 255 * componentColors[0])
                                                  green:(255 - 255 * componentColors[1])
                                                   blue:(255 - 255 * componentColors[2])
                                                  alpha:componentColors[3]] autorelease];            
        } else if (numComponents == 2) {
            inverseColor = [[[UIColor alloc] initWithRed:(255 - 255 * componentColors[0])
                                                   green:(255 - 255 * componentColors[0])
                                                    blue:(255 - 255 * componentColors[0])
                                                   alpha:componentColors[1]] autorelease];    
        }


        cell.contentView.backgroundColor = cellColor;
        cell.textLabel.textColor = inverseColor;
        cell.textLabel.text = colorName;

    } else {
        cell.contentView.backgroundColor = [UIColor whiteColor];        
        cell.textLabel.text = [NSString stringWithFormat:@"Unknown color (%@)", colorName];
        cell.textLabel.textColor = [UIColor blackColor];
    }
    cell.textLabel.backgroundColor = cell.contentView.backgroundColor;

    return cell;
}

彩色 uitableview 单元格

于 2012-05-10T13:28:44.023 回答
0

要在 iPhone 应用程序中更改 UITableView 的背景颜色,只需将给定的代码行写入 tableView cellForRowAtIndexPath。

 tableView.backgroundColor = [UIColor grayColor];

//设置选定的背景颜色

UIImageView *selectedBackground = [[UIImageView alloc] initWithFrame:CGRectMake(0,0, 320,100)];
selectedBackground.backgroundColor = [UIColor orangeColor];
[cell setSelectedBackgroundView:selectedBackground];

对于示例图像....

在此处输入图像描述

在此处输入图像描述

于 2012-05-10T12:07:08.417 回答
0

您可以使用 NSDictionary 类来存储颜色,字符串颜色将是您需要的颜色的关键,即

NSDictionary *colorsDict = [NSDictionary dictionaryWithObjectsAndKeys:
                                               [UIColor redColor],@"Red Color",
                                               [UIColor greenColor],@"Green Color",
                                               [UIColor blueColor],@"Blue Color",nil];
tableView.backgroundColor = [colorsDict valueForKey:color];

字符串颜色必须等于任何键。

于 2012-05-10T14:27:48.580 回答