如何设置tableview背景颜色?

时间:2012-05-10 12:03:46

标签: iphone objective-c ios

我知道我会问一个愚蠢的问题,但请提供一些答案。

如果我有表格视图,我想设置表格视图的背景颜色,我有一个名为color的字符串,其值为"gray Color"

如何在字符串color的帮助下设置表格视图的背景颜色。

如需澄清,请回复我。

感谢。

3 个答案:

答案 0 :(得分:1)

这里有一些您可以用来开始使用的代码。我使用了所有UIColor's类颜色来保持代码简洁。如果您需要确定其他自定义颜色名称,例如“灰色颜色”,那么您会被要求为要处理的每种颜色强制编写一个long 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;
}

colored uitableview cells

答案 1 :(得分: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];

样本图像......

enter image description here

enter image description here

答案 2 :(得分:0)

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

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

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