59 votes

iPhone: UITableViewCell: changement de la couleur d'arrière-plan de la cellule sélectionnée

Est-ce que quelqu'un sait comment changer la couleur de fond d'une cellule en utilisant UITableViewCell, pour chaque cellule sélectionnée? J'ai créé cette UITableViewCell dans le code pour TableView.

96voto

iPhoney Points 5473

La modification de la propriété selectedBackgroundView est la méthode la plus simple et la plus correcte. J'utilise le code suivant pour changer la couleur de sélection:

 // set selection color
UIView *myBackView = [[UIView alloc] initWithFrame:cell.frame];
myBackView.backgroundColor = [UIColor colorWithRed:1 green:1 blue:0.75 alpha:1];
cell.selectedBackgroundView = myBackView;
[myBackView release];
 

40voto

loomer Points 1525

J'ai finalement réussi à faire en sorte que cela fonctionne dans une vue tableau avec un style défini sur Groupé.

Commencez par définir la propriété selectionStyle de toutes les cellules sur UITableViewCellSelectionStyleNone .

 cell.selectionStyle = UITableViewCellSelectionStyleNone;
 

Ensuite, implémentez les éléments suivants dans votre délégué de vue de table:

 static NSColor *SelectedCellBGColor = ...;
static NSColor *NotSelectedCellBGColor = ...;

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSIndexPath *currentSelectedIndexPath = [tableView indexPathForSelectedRow];
    if (currentSelectedIndexPath != nil)
    {
        [[tableView cellForRowAtIndexPath:currentSelectedIndexPath] setBackgroundColor:NotSelectedCellBGColor];
    }

    return indexPath;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [[tableView cellForRowAtIndexPath:indexPath] setBackgroundColor:SelectedCellBGColor];
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (cell.isSelected == YES)
    {
        [cell setBackgroundColor:SelectedCellBGColor];
    }
    else
    {
        [cell setBackgroundColor:NotSelectedCellBGColor];
    }
}
 

19voto

Pranesh231286 Points 373
// animate between regular and selected state
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {

    [super setSelected:selected animated:animated];

    if (selected) {
        self.backgroundColor = [UIColor colorWithRed:234.0f/255 green:202.0f/255 blue:255.0f/255 alpha:1.0f];
    }
    else {
        self.backgroundColor = [UIColor clearColor];
    }
}

12voto

Salim Points 3017
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

     UITableViewCell *cell = (UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
     cell.contentView.backgroundColor = [UIColor yellowColor];

}

11voto

Yerkezhan Points 99

J'ai créé UIView et défini la propriété de la cellule selectedBackgroundView:

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

Prograide.com

Prograide est une communauté de développeurs qui cherche à élargir la connaissance de la programmation au-delà de l'anglais.
Pour cela nous avons les plus grands doutes résolus en français et vous pouvez aussi poser vos propres questions ou résoudre celles des autres.

Powered by:

X