0

When I load a TableView the default cell background color of the highlighted cell is light grey. How and where do I change this to another color?

Edward Hasted
  • 3,201
  • 8
  • 30
  • 48
  • are you loading from nib or through you are trying to create the instance of cell? – Rajesh Mar 04 '14 at 06:51
  • 1
    See this post: http://stackoverflow.com/a/2025099/1694129 – Javi Campaña Mar 04 '14 at 06:54
  • 1
    http://stackoverflow.com/questions/2418189/iphone-uitableviewcell-changing-background-color-of-selected-cell – Hamdullah shah Mar 04 '14 at 06:55
  • Have you scanned the docs for `UITableViewCell`? – rmaddy Mar 04 '14 at 06:56
  • Will work through these later - many thanks - it's the highlighted colour not the general background. When I press a cell I can dictate it with code like UIView *selectionView = [[UIView alloc] init]; selectionView.backgroundColor = [UIColor greenColor]; cell.selectedBackgroundView = selectionView; but it's the first time it loads that it defaults to gray. – Edward Hasted Mar 04 '14 at 07:36

2 Answers2

1

We can set selected backgroundView in this method

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

with thesse lines

UIView *selectionView = [[UIView alloc] init];
selectionView.backgroundColor = [UIColor greenColor];
cell.selectedBackgroundView = selectionView;
sehaswaran
  • 141
  • 1
  • 8
  • No - that routine works fine when you move cell focus after the page has loaded. My questions is how you alter the background colour of the selected cell when the page loads. The default is gray and I want to change it to say yellow. – Edward Hasted Mar 05 '14 at 09:34
  • Getting muddled: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath paints the screen when the page loads and - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath deals with each time you press a new cell. Apologies – Edward Hasted Mar 05 '14 at 10:22
1

Set selectionStyle for cell in method:

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

// Code to initialize your cell
    cell.selectionStyle = UITableViewCellSelectionStyleBlue;
    return cell;

}
Ab'initio
  • 5,368
  • 4
  • 28
  • 40