I need to change the default blue color selection of table view to some custom color. Is there any way to do that. Help me
-
12I think you would have got better results if you added even more exclamation marks... – Kuba Suder Oct 30 '09 at 12:38
5 Answers
The best way to do this is like this:
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"myCellId";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
UIView *v = [[[UIView alloc] init] autorelease];
v.backgroundColor = [UIColor redColor];
cell.selectedBackgroundView = v;
}
// Set up the cell...
cell.textLabel.text = @"foo";
return cell;
}
The relevant part for you is the cell.selectedBackgroundView = v;
instruction.
You can substitute the very basic view 'v' here with any view you like.

- 10,293
- 6
- 33
- 35
-
1Zoran, I have verified that this approach works for plain table views, however it does not work for my grouped ones. Any workaround? Or can you confirm this? – Massimo Cafaro Sep 28 '09 at 13:07
-
It's not dependent of the table view type: I have just discovered that it works on some plain tables and does not on other plain tables. All of them use custom table cells. – Massimo Cafaro Sep 28 '09 at 13:22
-
1This only works with no fuzz for plain table views. For a grouped table view you will actually need 4 views for different selected backgrounds; topmost, bottommost, between two other cells, and single cell in group. Then comes the pain of updating these you change the number of rows in any segment. Post a feature request on bugreport.apple.com to add a "selectedBackgroundGradient" property for iPhone OS 4.0. – PeyloW Sep 28 '09 at 14:56
-
Thank you PeyloW for clarifying this. Indeed, I had no success at all with grouped tables, and some successes with plain tables. But I still do not understand why for some of my plain tables this works and, for other, it does not. – Massimo Cafaro Sep 28 '09 at 15:02
I also came across this problem with trying to create custom selected cell views for the grouped cell. I solved this problem by creating 3 types of images for the cell top, middle and bottom assuming that there will be a middle cell.
NSString *imageFile;
if (row == 0) {
imageFile = @"highlighted_cell_top.png";
} else if (row == ([registeredDetailsKeys count] - 1)) {
imageFile = @"highlighted_cell_bottom.png";
} else {
imageFile = @"highlighted_cell_middle.png";
}
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageFile]];
cell.selectedBackgroundView = imageView;
There is possibly an easier way but I am happy with the result.

- 243
- 2
- 8
-
As far as I know, that's exactly the way to do it. See this [tutorial](http://cocoawithlove.com/2009/04/easy-custom-uitableview-drawing.html). You've forgotten the "top_bottom" variation (when only a single cell), that's all. – Rich Feb 01 '12 at 16:58
I do not think you can use a custom color. However, you can use the following property of UITableViewCell
@property(nonatomic) UITableViewCellSelectionStyle selectionStyle
The selection style is a backgroundView constant that determines the color of a cell when it is selected. The default value is UITableViewCellSelectionStyleBlue. Since
typedef enum {
UITableViewCellSelectionStyleNone,
UITableViewCellSelectionStyleBlue,
UITableViewCellSelectionStyleGray
} UITableViewCellSelectionStyle;
you can switch from the default blue to gray, or no colored selection at all.

- 25,429
- 15
- 79
- 93
-
1If you subclass your UITableViewCell, you can highlight in any color. – mahboudz Sep 28 '09 at 07:10
-
How exactly do you do this for custom UITableViewCells designed with IB or programmatically? I mean, is there any method/property that allows doing this WITHOUT recurring to tricks such as putting another view on top of the cell? – Massimo Cafaro Sep 28 '09 at 07:59
-
can u please send me the code to subclass the UITableViewCell and highlight it with a custom color. please.. – Sep 28 '09 at 08:42
-
By convention, you should avoid subclassing UITableViewCell. In any case, the technique to highlight with a custom colour doesn't require subclassing, but although simple enough is not quite as simple as one first thinks. Ufb007 explains it prefectly. – Rich Feb 01 '12 at 16:54
Make sure that after you declare in the header
@property(nonatomic) UITableViewCellSelectionStyle selectionStyle
Implement
cell.selectionStyle = UITableViewCellSelectionStyleGray
where UITableViewCellSelectionStyleGray
can be UITableViewCellSelectionStyleNone
, UITableViewCellSelectionStyleBlue
, UITableViewCellSelectionStyleGray
.

- 41
- 1
- 2
Another way to do it would be to move in a new view over your cell, with whatever color you'd like and 50% or so opacity. You move this view over to the cell when you get a -setSelected:animated: call. When I say move, you actually could always have a view on top of your cell, but just turn the hidden bit off and on as you need.

- 39,196
- 16
- 97
- 124