2012-05-10 13 views
0

Я знаю, что собираюсь задать глупый вопрос, но, пожалуйста, предоставьте какой-то ответ.Как установить цвет фона стола?

Если у меня есть вид таблицы, я хочу установить цвет фона в виде таблицы, и у меня есть строка с именем color, которые имеют значение "gray Color".

Как установить фон Цвет стола с помощью строки color.

Для уточнений, пожалуйста, вернитесь ко мне.

Спасибо.

ответ

0

Чтобы изменить цвет фона в UITableView в приложении iPhone, вам просто нужно написать данную строку кода в 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

+0

@ Unknown .. Я спрашиваю, есть ли у меня строка с именем цвета, а затем, как установить это имя на цвет фона стола. e g: У меня есть строка «color», значение которой «redColor». Могу ли я установить ее как tableView.backGroundColor = name; – vshall

+0

[Что вы пробовали] (http://mattgemmell.com/2008/12/08/what-have-you-tried/) – akk

+0

@vishal Нет, вы не можете –

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

0

Вы можете использовать класс NSDictionary для хранения цветов и струнного цвета будет иметь ключевое значение для цвета, что вам нужно, т.е.

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

Строка цвет должен быть равен любым ключу.

Смежные вопросы