Reputation: 619
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.
Upvotes: 0
Views: 328
Reputation: 10096
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. 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.
- (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;
}
Upvotes: 1
Reputation: 916
You can use NSDictionary class to store colors and string color will be key for color that you need i.e.
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.
Upvotes: 0
Reputation: 2218
To change the background color of UITableView in iPhone application, you just need to write the given line of code into the 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....
Upvotes: 0