2015-09-04 90 views
1

我在viewDidLoad这样的一个数组,它包含UIColor将被用于填充表格视图单元格背景颜色如何用数组中的颜色填充表格视图单元格背景?

self.ColorArray = [NSArray arrayWithObjects: 
         @"[UIColor redColor];", 
         @"[UIColor redColor];", 
         @"[UIColor redColor];", nil]; 

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    cell.contentView.backgroundColor = [self.ColorArray objectAtIndex: indexPath.row % self.ColorArray.count]; 
} 

但这种崩溃掉给抛出这个错误:

-[__NSCFConstantString CGColor]: unrecognized selector sent to instance 0x137b28 - 
+0

检查码与该变形例self.ColorArray = [NSArray的arrayWithObjects: [的UIColor redColor], [的UIColor redColor], [的UIColor redColor],零]。 –

回答

0

要初始化使用字符串对象的颜色阵列

请尝试向阵列添加UIColor对象

self.ColorArray = @[[UIColor whiteColor], @[UIColor redColor], @[UIColor grayColor]];

1

您试图将颜色作为字符串传递。

尝试如通过:

self.ColorArray = [NSArray arrayWithObjects: 
    [UIColor redColor], 
    [UIColor redColor], 
    [UIColor redColor], nil]; 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 

    cell.contentView.backgroundColor = [self.ColorArray objectAtIndex: indexPath.row % self.ColorArray.count]; 

} 
+0

好的,你能告诉我如何使用常量做这个用法吗? –

+0

#define Color_tblRow1 [UIColor colorWithRed:0.518 green:0.11 blue:0 alpha:1];/*#841c00 */ –

+0

它不允许我将color_tblRow1添加到数组中 –

相关问题