2010-05-20 3 views
2

J'essaie de changer l'accessoire customView d'une uitableviewcell immédiatement après que l'utilisateur ait cliqué sur la cellule. Comment ferais-je cela?Modification d'un accessoire personnaliséAfficher dans une uitableviewcell?

Pour mémoire, je suis grâce à la vue de la table personnalisée « Matt Gallagher tutoriel:

http://cocoawithlove.com/2009/04/easy-custom-uitableview-drawing.html

lien de téléchargement pour la source: http://projectswithlove.com/projects/EasyCustomTable.zip


EDIT

Essayé ce code , mais cela change juste l'accessoire AFTER retouche. A également essayé willSelectRowAtIndexPath avec le même résultat.

- (NSIndexPath *)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    UIImage *indicatorImage = [UIImage imageNamed:@"indicatorSelected.png"]; 
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath]; 
    cell.accessoryView = [[[UIImageView alloc] initWithImage:indicatorImage] autorelease]; 
    return indexPath; 
} 

EDIT

Problème résolu en faisant l'image de fond de la cellule contient les accessoires. si l'accessoire était « truqué » en en faisant partie de l'image

Répondre

1

Vous pouvez changer la accessoryView dans la méthode de sélection comme indiqué:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    cell.accessoryView = newView; 
} 

Cela prend juste la cellule courante qui a été sélectionné et modifie la vue accessoire de celui-ci. Votre objet d'affichage irait où newView est.

+0

Pas assez de travail pour moi. Voir la question mise à jour – cannyboy

6

vous pouvez peut-être essayer de recharger la cellule dans willSelectRowAtIndexPath/didSelectRowAtIndexPath:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    UIImage *indicatorImage = [UIImage imageNamed:@"indicatorSelected.png"]; 
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath]; 
    cell.accessoryView = [[[UIImageView alloc] initWithImage:indicatorImage] autorelease]; 
    [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:NO]; 
} 

Une autre recommandation: didSelectRowAtIndexPath retourne void pas NSIndexPath.

+0

willSelectRowAtIndexPath renvoie NSIndexPath *, non vide. – oltman

+0

Vous avez raison. C'était mon erreur avec 'willSelectRowAtIndexPath' (édité). Mais 'didSelectRowAtIndexPath' renvoie void. ** Avez-vous juste -1 ** parce que cette recommandation est fausse pour ('willSelectRowAtIndexPath') ou ** y a-t-il aussi une erreur ** dans l'exemple d'implémentation? –

0

Utilisez la propriété highlightedImage de UIImageView:

UIImageView* arrowView = [[UIImageView alloc] initWithImage:normalImage]; 
arrowView.highlightedImage = selectedImage; 
cell.accessoryView = arrowView; 
[arrowView release]; 
Questions connexes