12
votes

Comment mettre en évidence une ligne dans un utailview

Le code ci-dessous semble avoir aucun effet. Je veux que cela soit mis en surbrillance de la même manière qu'il met en évidence lorsque vous appuyez sur une ligne

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
   ...
   [cell.textLabel setHighlighted:YES];


   return cell;
}


1 commentaires

Ce Post pourrait aider.


6 Réponses :


31
votes

Cette ligne gérera de repeindre la cellule, l'étiquette et l'accessoire pour vous:

[tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];


0 commentaires

1
votes

Vous avez deux choix. Utilisez la sélection de la vue Table: xxx pré>

ou modifier la couleur d'arrière-plan de la cellule de table dans la vue Table View Délégué: P>

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    cell.backgroundColor = ...
}


0 commentaires

15
votes
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection: 0];

[tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
usually worksIf you are using interface builder to build your interface, one thing to check is that you specified the view of the table view controller. If you don't actually specify the view, this selection message may go nowhere.(you specify the view by selecting the table view controller in the interface builder inspector window and dragging the outlet for "view" to the table view you want to do the selection in).

0 commentaires

2
votes

Vous pouvez utiliser

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
   ...
   [cell.textLabel setHighlighted:YES];


   return cell;
}


0 commentaires

3
votes
- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
   return YES;
}

- (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
  // do something here
}
Try this :)


21
votes

Comme d'autres l'ont noté, vous pouvez sélectionner programmatimatiquement une cellule à l'aide de cette méthode: xxx

Cependant, vous devriez faire attention pour ne pas appeler cette méthode trop tôt.

La première fois que le contrôleur d'affichage contenant ledit tableview est initialisé , le plus tôt que vous devez appeler cette méthode est dans ViewDidAppear: < /code ,.

so, vous devez faire quelque chose comme ceci: xxx

si vous essayez de mettre cet appel dans ViewDidLoad , ViewWillappear: ou tout autre appel de cycle de vie anticipée, il ne fonctionnera probablement pas ni des résultats étranges (tels que le défilement à la position correcte mais ne pas sélectionner la cellule). >


1 commentaires

Sur iOS 9 semble fonctionner à partir de ViewWillapear et viewDidAppear et non de ViewDidLoad .