fannheyward
fannheyward

Reputation: 19307

How to change the color of UITableViewCell selected view?

How to change the gray color when cell been selected?

enter image description here

Upvotes: 0

Views: 4275

Answers (5)

Raju
Raju

Reputation: 759

When user click on Selected Row

(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath    
{
     UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
     cell.contentView.backgroundColor = [UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:1.0];
}

Upvotes: 3

Ashutosh Dubey
Ashutosh Dubey

Reputation: 385

Try this :

cell.selectionStyle = UITableViewCellSelectionStyleBlue;

Upvotes: 1

fannheyward
fannheyward

Reputation: 19307

Thanks @Kjuly and @Selkie's answer. I make it works with both of you.

  1. set selectedBackgroundView's backgroundColor first.
  2. change cell.textLabel.backgroundColor and cell.contentView.backgroundColor at didSelectedRowAtIndexPath.

Thank you again.

Upvotes: 1

Kjuly
Kjuly

Reputation: 35181

Set the selectedBackgroundView's color as what you want in your custom tableview cell (which is a subclass of UITableViewCell):

UIView * selectedBackgroundView = [[UIView alloc] initWithFrame:self.frame];
[selectedBackgroundView setBackgroundColor:[UIColor redColor]]; // set color here
[self setSelectedBackgroundView:selectedBackgroundView];
[selectedBackgroundView release];

or you can configure it in -tableView:cellForRowAtIndexPath: method:

//...
[cell setSelectedBackgroundView:selectedBackgroundView];
//...

Upvotes: 2

Selkie
Selkie

Reputation: 1783

How about change the background color in delegate method:

-(void)tableView:(UITableView *)tableView didSelectedRowAtIndexPath:(NSIndexPath *)indexPath

Upvotes: 1

Related Questions