0

Is it possible to programmatically set (from the awakeFromNib method) the background color of a portion of a custom table view cell when the cell is selected.

My attempt so far highlights the entire cell in red

UIView *bgColorView = [[UIView alloc] init];
bgColorView.backgroundColor = [UIColor redColor];
[self setSelectedBackgroundView:bgColorView];
grabury
  • 4,797
  • 14
  • 67
  • 125
  • may be you can use gradients, rather than a flat red color? or create a uiview with the size of the red you want and insert it above the background? – Veeru Jul 24 '14 at 08:02
  • see this http://stackoverflow.com/questions/18128497/changing-only-part-of-a-custom-uitableviewcell-selected-background-color/18140257#18140257 – Shankar BS Jul 24 '14 at 08:47

1 Answers1

0

If this is acceptable, modifying your code a little.

UIView *bgColorView = [[UIView alloc] init];
bgColorView.frame=CGRectMake(0,0,200,100); //this could be replaced with numbers to cover portion of your cell
bgColorView.backgroundColor = [UIColor redColor];
[self setSelectedBackgroundView:bgColorView];
Veeru
  • 4,936
  • 2
  • 43
  • 61
  • I've tried that but the entire cell is highlighted red. Setting the frame doesnt change anything – grabury Jul 24 '14 at 08:11
  • Setting view as background will fill the view, rather than setting it as background, can you try insertSubViewAtIndex:1? Dont have a mac right now to test it out. – Veeru Jul 24 '14 at 08:13
  • The red is added to all the cells. I only want the selected cell to be red. I didnt make this clear in the question. Sorry – grabury Jul 24 '14 at 08:20
  • Ah, ok - you can use that code, probably refer to http://stackoverflow.com/questions/1998775/uitableview-cell-selected-color – Veeru Jul 24 '14 at 08:38