Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Altering the background color of cell.accessoryView and cell.editingAccessoryView

Whenever I add an accessoryView to my UITableViewCell, it doesn't carry the background color across? I'm setting a UISwitch as my accessoryView, and the color I have set in the cell.backgroundColor property only effects the contentView and not the accessoryView. I have tried everything to set them to the same value. I tried to set the cell.backgroundView.backgroundColor and the cell.accessoryView.backgroundColor properties to the color I want but nothing is working. I also tried creating a subview inside contentView, which solved the backgroundColor problem (by avoiding it), but it creates the problem, where the switch sits on top of the cell.textLabel when the text is too long.

Is there are way I can modify the background color of the accessoryView without creating a subview in contentView, or to alter the length of the cell.textLabel without subclassing UITableViewCell?

like image 402
Zak Avatar asked Aug 14 '10 18:08

Zak


2 Answers

Upon reading the documentation (a novel idea), I found the article, "A Closer Look at Table-View Cells". It helped me understand the composition of the cells, and I found my answer...

cells look like this...

alt text

Since the cell.accessoryView is a sister view to cell.contentView I had to ask the cell.contentView for its superview, and then I was able to change the background color for both views at once. Here's what the code looks like...

// Cell Formatting cell.contentView.superview.backgroundColor = [UIColor greenColor]; 

I know it's really simple, but I'm a newbie and it took me ages to slow down and read the doc. Hopefully, this helps some other folks out there!

like image 70
Zak Avatar answered Sep 23 '22 13:09

Zak


- (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {     cell.backgroundColor = [UIColor yellowColor]; } 
like image 23
Aalok Bhatwal Avatar answered Sep 21 '22 13:09

Aalok Bhatwal