user169964
user169964

Reputation:

Need to change the color of table view selection

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

Upvotes: 24

Views: 24019

Answers (5)

dimmalo
dimmalo

Reputation: 43

Make sure that after you declare in the header

@property(nonatomic) UITableViewCellSelectionStyle selectionStyle

Implement

cell.selectionStyle = UITableViewCellSelectionStyleGray

where UITableViewCellSelectionStyleGray can be UITableViewCellSelectionStyleNone, UITableViewCellSelectionStyleBlue, UITableViewCellSelectionStyleGray.

Upvotes: 3

Ufb007
Ufb007

Reputation: 253

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.

Upvotes: 6

Zoran Simic
Zoran Simic

Reputation: 10333

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.

Upvotes: 61

mahboudz
mahboudz

Reputation: 39376

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.

Upvotes: 0

Massimo Cafaro
Massimo Cafaro

Reputation: 25419

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.

Upvotes: 3

Related Questions