Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

UITableView Cell selected Color?

I have created a custom UITableViewCell. The table view is showing data fine. What I am stuck in is when user touches cell of tableview, then I want to show the background color of the cell other than the default [blue color] values for highlighting the selection of cell. I use this code but nothing happens:
cell.selectedBackgroundView.backgroundColor=[UIColor blackColor];
by

2 Answers

akshay1995
Objective-C:

UIView *bgColorView = [[UIView alloc] init];
bgColorView.backgroundColor = [UIColor redColor];
[cell setSelectedBackgroundView:bgColorView];

Swift:

let bgColorView = UIView()
bgColorView.backgroundColor = UIColor.red
cell.selectedBackgroundView = bgColorView
RoliMishra
If you have a grouped table with just one cell per section, just add this extra line to the code:
bgColorView.layer.cornerRadius = 10;


UIView *bgColorView = [[UIView alloc] init];
[bgColorView setBackgroundColor:[UIColor redColor]];
bgColorView.layer.cornerRadius = 10;
[cell setSelectedBackgroundView:bgColorView];
[bgColorView release];

Login / Signup to Answer the Question.