Manal
Manal

Reputation: 1

change UITableViewCell background color when selected

i have a UItableview and i want to change the cell background when it is seleceted and keep it like that so i tried to use [cell setBackgroundColor:[UIColor purpleColor]]; but that made other cells get colored at same time and i dont know why i also tried to use [cell.textLabel setBackgroundColor:[UIColor purpleColor]]; but when i select another cell the background go back to white color so any ideas for solving that problem??

Upvotes: 0

Views: 2013

Answers (3)

Shinigamae
Shinigamae

Reputation: 862

Subclass UITableViewCell by yourself class, i.e. MyTableViewCell and reimplement the selected function.

-(void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
    UIView *backgroundView = [[UIView alloc] initWithFrame:self.selectedBackgroundView.frame];
    [backgroundView setBackgroundColor:[UIColor purpleColor]];
    [self setSelectedBackgroundView:backgroundView];
}

Upvotes: 0

Brandon Frohbieter
Brandon Frohbieter

Reputation: 18139

since 3.0, set it in the willDisplayCell method:

 - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
cell.backgroundColor = [UIColor redColor];

}

Upvotes: 0

ySgPjx
ySgPjx

Reputation: 10245

UIView *backgroundView = [[UIView alloc] initWithFrame:cell.selectedBackgroundView.frame];
[backgroundView setBackgroundColor:[UIColor purpleColor]];
[cell setSelectedBackgroundView:backgroundView];
[backgroundView release];

Upvotes: 3

Related Questions