warzone_fz
warzone_fz

Reputation: 482

How to fill table view cell background with colors from an array?

I have an array like this in viewDidLoad which contains UIColor that would be used to fill table view cell background colour

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]; 
}

But this crashes away giving throws this error :

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

Upvotes: 1

Views: 885

Answers (2)

iBhavin
iBhavin

Reputation: 1261

You are trying to pass color as string.

Try to pass as :

  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]; 

}

Upvotes: 1

GoodSp33d
GoodSp33d

Reputation: 6282

You are initializing color array with String objects

Instead try adding UIColor objects to the array

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

Upvotes: 0

Related Questions