简体   繁体   English

如何设置tableview的背景色?

[英]How to set tableview background color?

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" . 如果我有表格视图,我想设置表格视图的背景色,并且有一个名为color的字符串,其值为"gray Color"

How to set background Color of table view with the help of string color . 如何在字符串color的帮助下设置表格视图的背景颜色。

For clarifications, Please get back to me. 有关澄清,请与我联系。

Thanks. 谢谢。

Here's a bit of code you may use to get started. 这是您可能会开始使用的一些代码。 I used all the UIColor's class colors to keep code short and clean. 我使用了所有UIColor's类颜色来保持代码简短简洁。 If you need to determine other custom color names, like "gray Color" you were asking you'll be forced to write a long if clause with and if statement for each color you want to handle. 如果您需要确定其他自定义颜色名称,例如“灰色颜色”,则要求您强制为要处理的每种颜色编写带有if和if语句的long 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单元

To change the background color of UITableView in iPhone application, you just need to write the given line of code into the tableView cellForRowAtIndexPath. 要在iPhone应用程序中更改UITableView的背景颜色,只需将给定的代码行写入tableView cellForRowAtIndexPath中。

 tableView.backgroundColor = [UIColor grayColor];

//To Set Selected Background Color //设置所选背景色

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

For sample image.... 对于示例图像。

在此处输入图片说明

在此处输入图片说明

You can use NSDictionary class to store colors and string color will be key for color that you need ie 您可以使用NSDictionary类存储颜色,而字符串颜色将是您需要的颜色的关键,即

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

String color must be equal to any key. 字符串颜色必须等于任何键。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM