2010-09-20 3 views
1

Je recherche cette option depuis un certain temps, mais je n'ai jamais trouvé de solution.Suppression de la couleur de surbrillance mais maintien de la surbrillance de l'image dans Custom UITableViewCell

J'ai un UITableView avec des cellules personnalisées, ces cellules ont une image et je place les étiquettes dessus. Le fait est que lorsque je sélectionne une cellule, la cellule entière est sélectionnée en utilisant la couleur de surbrillance par défaut. J'ai essayé de changer le style, mais si j'utilise cell.selectionStyle = UITableViewCellSelectionStyleNone; l'image ne sera pas en surbrillance ...

Juste au cas où quelqu'un me demande j'ai créé des images distinctes pour les états surlignés/normaux.

Répondre

1

Une propriété est mise en surbrillance dans UITableViewCell. Par conséquent, vous pouvez modifier le comportement en surbrillance en écrasant cette méthode.

- (void)setHighlighted:(BOOL)value 
{ 
    [super setHighlighted:value]; 

    // Do custom highlighted behavior here 
    ... 
} 

ou écraser méthode setHighlighted:animated:.


Something like this? 

In MyCustomCell.m 

- (void)setHighlighted:(BOOL)value 
{ 
    [super setHighlighted:value]; 

    // Do custom highlighted behavior here 
    if (value == YES) 
    { 
     // show highlighted image 
    } else { 
     // show image for normal state. 
    } 

    // propagate the highlighted message to other views. 
    [mChildView setHighlighted:value]; 
} 
+0

Pourriez-vous me donner un code sur la façon de le faire? Mettez l'image en surbrillance mais ne mettez pas en surbrillance la cellule –

0

J'ai trouvé une très bonne solution.

Je n'avais pas à remplacer les méthodes.

1 - Dans mon CustomCell.xib j'ai créé 2 imagesViews. Un que je me suis connecté à myImageView outlet, et l'autre à selectedBackgroundView.

2 - Pour l'état normal, j'ai mis myCustomBackgroundImg.png et dans l'image en surbrillance j'ai mis un png vide, donc il ne restera pas au-dessus du backgroundView quand il est sélectionné.

3 - Dans la vue arrière-plan sélectionnée, je place myCustomSelectedBackgroundImg.png dans l'image d'état normal.

Espérons que cela aide les autres.

1

Voilà comment je l'ai fait travailler:

J'utilise un pour ma cellule XIB au catalogue. Là-bas, j'ai une image d'arrière-plan (non connectée à aucune prise) qui est mon arrière-plan d'état normal. J'ai mis cette image pour avoir aussi une image en surbrillance (une version bleue dans mon cas).

J'ai ajouté un autre UIView, défini sa couleur de fond pour effacer, et l'ai attaché à la prise de vue arrière-plan sélectionnée de la cellule. Cela empêche le comportement normal intégré "définir l'ensemble du fond bleu".

Ensuite, je mets le style de sélection de la cellule au bleu (ne peut pas être None car la mise en surbrillance n'aura pas lieu alors).

0

Amélioration du code de Toro.

Cela fonctionne!

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated { 

    if (highlighted == YES) { 
     // show highlighted image 
     [UIView animateWithDuration:0.1f animations:^{ 
      self.imageViewBanner.alpha = 0.3; 
     }]; 
    } else { 
     // show image for normal state. 
     [UIView animateWithDuration:0.1f animations:^{ 
      self.imageViewBanner.alpha = 1.0; 
     }]; 
    } 

    [super setHighlighted:highlighted animated:animated]; 
} 
0

meilleure solution que je pouvais trouver était de remplacer le selectedBackgroundView de la cellule avec une vue transparente, et en laissant selectionStyle à UITableViewCellSelectionStyleBlue, de sorte que mis en évidence l'état est encore propagée à tous les sous-vues (avec arrêts UITableViewCellSelectionStyleNone propagation en surbrillance à subviews).

UIView *transparentBackground = [[UIView alloc] initWithFrame:cell.bounds]; 
transparentBackground.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; 
transparentBackground.backgroundColor = [UIColor clearColor]; 
cell.selectedBackgroundView = transparentBackground; 
0

travail 100% et une solution simple:

Etape 1: définir cell.selectionStyle = UITableViewCellSelectionStyleNone; en code ou en InterfaceBuilder.
Étape 2: ajouter 2 méthodes:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath { 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    cell.imageView.highlighted = YES; 
} 

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    cell.imageView.highlighted = NO; 
} 

à Swift:

cell?.selectionStyle = .None 


override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    let cell = tableView.cellForRowAtIndexPath(indexPath) 
    cell?.imageView?.highlighted = true; 
} 

override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) { 
    let cell = tableView.cellForRowAtIndexPath(indexPath) 
    cell?.imageView?.highlighted = false; 
} 
Questions connexes