views:

2987

answers:

5

Hi All, 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

Thanks in Advance Shibin

+1  A: 

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.

unforgiven
If you subclass your UITableViewCell, you can highlight in any color.
mahboudz
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?
unforgiven
can u please send me the code to subclass the UITableViewCell and highlight it with a custom color.please..
Shibin Moideen
A: 

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.

mahboudz
+14  A: 

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.

Zoran Simic
Zoran, 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?
unforgiven
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.
unforgiven
This 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
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.
unforgiven
A: 

!!! OMG THANK YOU

UIView *v = [[[UIView alloc] init] autorelease];
v.backgroundColor = [UIColor redColor];
cell.selectedBackgroundView = v;

is the answer!!!

!!!

Chris Allinson
A: 

Very good answer, do you know how to stop buttons on a custom cell being selected?

Burf2000