Вот немного кода, вы можете использовать для начала. Я использовал все цвета класса 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;
}
@ Unknown .. Я спрашиваю, есть ли у меня строка с именем цвета, а затем, как установить это имя на цвет фона стола. e g: У меня есть строка «color», значение которой «redColor». Могу ли я установить ее как tableView.backGroundColor = name; – vshall
[Что вы пробовали] (http://mattgemmell.com/2008/12/08/what-have-you-tried/) – akk
@vishal Нет, вы не можете –