Set background color of UITableViewCell

I looked around to find a solution to set the background color of the accessory view to the same background color as the contents of the contentView.

cell.contentView.backgroundColor = [UIColor colorWithRed:178/255.f green:14/255.f blue:12/255.f alpha:0.05]; cell.accessoryView.backgroundColor =[UIColor colorWithRed:178/255.f green:14/255.f blue:12/255.f alpha:0.05]; 

There is a solution that works, but allows me to use only one color for all cells.

 cell.contentView.superView.backgroundColor = [UIColor redColor]; 

Is the only solution not to use the accessory and use the image instead?

Thanks!

+8
objective-c uitableview
source share
6 answers

For too long I struggled with this and resorted to creating a custom image with an accessory. But I found this solution that works well and does not require a special image. The trick is to change the color of the backgroundView element, not the backgroundColor.

 UIView *myView = [[UIView alloc] init]; if (indexPath.row % 2) { myView.backgroundColor = [UIColor whiteColor]; } else { myView.backgroundColor = [UIColor blackColor]; } cell.backgroundView = myView; 

No need to change the background colors of the accessory or contentView. They will automatically follow.


Note for 2014. Very often you use using - (void) setSelected: (BOOL) selected animated: (BOOL) animated

So, you would have your own class of cells, and you would set the colors for normal / selected like this ...

 HappyCell.h @interface HappyCell : UITableViewCell @property (strong, nonatomic) IBOutlet UILabel *mainLabel; etc... @end HappyCell.m @implementation HappyCell -(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { } return self; } -(void)awakeFromNib { } - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; if(selected) { self.backgroundColor = [UIColor redColor]; .. other setup for selected cell } else { self.backgroundColor = [UIColor yellowColor]; .. other setup for normal unselected cell } } @end // to help beginners....... // in your table view class, you'd be doing this... -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return yourDataArray.count; } -(UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSInteger thisRow = indexPath.row; ContentsCell *cell = [tv dequeueReusableCellWithIdentifier:@"cellName" forIndexPath:indexPath]; // "cellName" must be typed in on the cell, the storyboard // it the "identifier", NOT NOT NOT the restorationID [cell setupForNumber: thisRow]; cell.mainLabel.text = yourDataArray[ thisRow ][@"whatever"]; cell.otherLabel.text = yourDataArray[ thisRow ][@"whatever"]; return cell; } 

hope this helps someone.

+18
source share
 - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { cell.backgroundColor = [UIColor greenColor]; } 

Using this UITableViewDelegate method, you can set the color of cells in different colors. Note that Apple explicitly advises you to make changes to the backgroundColor property in the tableView:willDisplayCell:ForRowAtIndexPath: method in documents that state:

If you want to change the background color of the cell, do it in tableView: willDisplayCell: forRowAtIndexPath: method of your table view delegate

In fact, in iOS 6, changes to a property from elsewhere (for example, the tableView:cellForRowAtIndexPath: method) would have no effect. This no longer looks like iOS 7, but Apple advises changing the property from within tableView:willDisplayCell:ForRowAtIndexPath: (without any explanation).

For alternating colors, do something like this example:

 - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.row % 2) { cell.backgroundColor = [UIColor yellowColor]; } else { cell.backgroundColor = [UIColor redColor]; } } 
+26
source share

This worked for me:

 cell.contentView.backgroundColor = [UIColor darkGreyColor]; 
+7
source share

For all lines with the same color

 cell.backgroundColor = [UIColor colorWithRed:6.0/255.0 green:122.0/255.0 blue:145.0/255.0 alpha:1.0f]; 

For two colors

 cell.backgroundColor = [UIColor colorWithRed:6.0/255.0 green:122.0/255.0 blue:145.0/255.0 alpha:1.0f]; if ((cell.backgroundColor = (indexPath.row % 2 == 0 ? [UIColor colorWithRed:6.0/255.0 green:122.0/255.0 blue:145.0/255.0 alpha:1.0f] : [UIColor colorWithRed:2.0/255.0 green:68.0/255.0 blue:80.0/255.0 alpha:1.0f]))){ cell.textLabel.textColor = [UIColor whiteColor]; } 
+2
source share

For anyone who might stumble upon this and want to set the UITableViewCell background to a pattern or texture , rather than a solid color, you can do it like this:

 - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { cell.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"pattern.png"]]; } 
0
source share

The best option is to have different backgrounds and something that probably wouldn’t be for your own implementation of TableViewCell, there you can put the logic to show everything you want based on content or index, etc.

-one
source share

All Articles