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.
Réponses
Trop de publicités?
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];
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];
}
}
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];
}
}
Salim
Points
3017
Yerkezhan
Points
99