✔ Checkmark ligne sélectionnée dans UITableViewCell

Je suis un débutant de développement iOS. Je veux append une coche à mon UITableViewCell quand il est sélectionné. La coche doit être supprimée lorsqu'une autre ligne est sélectionnée. Comment ferais-je cela?

N'utilisez pas [tableview reloadData]; // c'est un marteau.

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark; } -(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone; } 

Dans votre méthode UITableViewDatasource:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSSsortingng *CellIdentifier = @"Cell"; UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if(cell == nil ) { cell =[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; } if ([indexPath compare:self.lastIndexPath] == NSOrderedSame) { cell.accessoryType = UITableViewCellAccessoryCheckmark; } else { cell.accessoryType = UITableViewCellAccessoryNone; } return cell; } // UITableView Delegate Method -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { self.lastIndexPath = indexPath; [tableView reloadData]; } 

Et lastIndexPath est une property(strong) NSIndexPath* lastIndexPath;

J'ai trouvé que recharger datatables interrompt l'animation désélectionner d'une manière laide.

Cette implémentation de Swift ajoute / supprime proprement des coches et désélectionne la ligne:

 func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { if self.lastSelection != nil { self.myTableView.cellForRowAtIndexPath(self.lastSelection)?.accessoryType = .None } self.myTableView.cellForRowAtIndexPath(indexPath)?.accessoryType = .Checkmark self.lastSelection = indexPath self.myTableView.deselectRowAtIndexPath(indexPath, animated: true) } 

lastSelection est déclaré comme var lastSelection: NSIndexPath!

Aucune activité supplémentaire dans cellForRowAtIndexPath nécessaire. Ne devrait pas être difficile à reproduire dans Obj-C.

Pour définir une coche:

 UITableViewCell *cell = ...; cell.accessoryType = UITableViewCellAccessoryCheckmark; 

Pour sélectionner / désélectionner une cellule:

 [cell setSelected:TRUE animated:TRUE]; // select [cell setSelected:FALSE animated:TRUE]; // deselect 

Pour désélectionner la cellule précédente, utilisez un ivar NSIndexPath * lastSelected pour suivre la dernière cellule sélectionnée:

 - (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath { if (self.lastSelected==indexPath) return; // nothing to do // deselect old UITableViewCell *old = [self.tableView cellForRowAtIndexPath:self.lastSelected]; old.accessoryType = UITableViewCellAccessoryNone; [old setSelected:FALSE animated:TRUE]; // select new UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath]; cell.accessoryType = UITableViewCellAccessoryCheckmark; [cell setSelected:TRUE animated:TRUE]; // keep track of the last selected cell self.lastSelected = indexPath; } 

petite faute de frappe

 // deselect old UITableViewCell *old = [self.tableView cellForRowAtIndexPath:self.lastSelected]; cell.accessoryType = UITableViewCellAccessoryNone; [cell setSelected:FALSE animated:TRUE]; 

Devrais lire

 // deselect old UITableViewCell *old = [self.tableView cellForRowAtIndexPath:self.lastSelected]; old.accessoryType = UITableViewCellAccessoryNone; [old setSelected:FALSE animated:TRUE]; 

et aussi dans le

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.row == [previouslySelected intValue]) { cell.accessoryType = UITableViewCellAccessoryCheckmark; selectedIndex = indexPath; [cell setSelected:YES animated:YES]; } else { cell.accessoryType = UITableViewCellAccessoryNone; [cell setSelected:NO animated:YES]; } } 

précédemmentSélectionné est votre ivar local, etc. De cette façon, si vous rechargez avec un index sélectionné, il est également désélectionné lorsque vous feuilletez les sélections possibles.

En supposant que vous êtes dans une class qui hérite de UITableViewController , cela fait l'affaire dans Swift 3:

 override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { // Add a visual cue to indicate that the cell was selected. self.tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark } override func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? { // Invoked so we can prepare for a change in selection. // Remove previous selection, if any. if let selectedIndex = self.tableView.indexPathForSelectedRow { // Note: Programmatically deslecting does NOT invoke tableView(:didSelectRowAt:), so no risk of infinite loop. self.tableView.deselectRow(at: selectedIndex, animated: false) // Remove the visual selection indication. self.tableView.cellForRow(at: selectedIndex)?.accessoryType = .none } return indexPath } 

Le code mentionné ci-dessus fonctionne uniquement avec la sélection unique . Ce code fonctionnera sûrement pour plusieurs sélections .

 - (void)viewDidLoad { arrSelectionStatus =[NSMutableArray array]; //arrSelectionStatus holds the cell selection status for (int i=0; i<arrElements.count; i++) { //arrElements holds those elements which will be populated in tableview [arrSelectionStatus addObject:[NSNumber numberWithBool:NO]]; } } -(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"]; if (cell==nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"]; } cell.textLabel.text=[arrElements objectAtIndex:indexPath.row]; if ([[arrSelectionStatus objectAtIndex:indexPath.row] boolValue] == YES) cell.accessoryType = UITableViewCellAccessoryCheckmark; else cell.accessoryType = UITableViewCellAccessoryNone; return cell; } -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; cell.accessoryType = UITableViewCellAccessoryCheckmark; [arrSelectionStatus replaceObjectAtIndex:indexPath.row withObject:[NSNumber numberWithBool:YES]]; } -(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{ UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; cell.accessoryType = UITableViewCellAccessoryNone; [arrSelectionStatus replaceObjectAtIndex:indexPath.row withObject:[NSNumber numberWithBool:NO]]; } 

Appelez didSelectRowAtIndexPath Méthode didSelectRowAtIndexPath lorsque vous select une ligne quelconque pour afficher CheckMark et select la ligne de coche pour masquer CheckMark.

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath { [tableView deselectRowAtIndexPath:indexPath animated:true]; NSLog(@"touch"); UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; if (cell.accessoryType == UITableViewCellAccessoryNone) { cell.accessoryType = UITableViewCellAccessoryCheckmark; } else { cell.accessoryType = UITableViewCellAccessoryNone; } } 

Quand une cellule sélectionnée (avec la coche est sélectionnée à nouveau), supprimez simplement la sélection.

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath { BOOL isSelected = ([tableView cellForRowAtIndexPath:indexPath].accessoryType == UITableViewCellAccessoryCheckmark); if(isSelected){ [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone; [tableView deselectRowAtIndexPath:indexPath animated:YES]; //this won't sortinggger the didDeselectRowAtIndexPath, but it's always a good idea to remove the selection }else{ [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark; } } - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath*)indexPath { [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone; } 

Prime:

Utilisez self.tableView.indexPathForSelectedRow pour détecter indexPath pour la cellule sélectionnée

Les réponses ci-dessus ne fonctionnent pas si vous avez réutilisé la cellule pour un grand nombre de données. Sur le défilement, vous pouvez voir la coche répétée. Pour éviter l'utilisation ci-dessous les étapes:

  1. déclarer sur variable: var indexNumber: NSInteger = -1

  2. Ajoutez le code ci-dessous dans cellforRowAtIndexPath:

      override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{ if indexNumber == indexPath.row{ cell.accessoryType = .checkmark }else{ cell.accessoryType = .none } } 
  3. Et dans didselectAtIndexpath append ci-dessous le code:

override func tableView (_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

 override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { tableView.cellForRow(at: indexPath as IndexPath)?.accessoryType = .checkmark indexNumber = indexPath.row } override func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) { tableView.cellForRow(at: indexPath as IndexPath)?.accessoryType = .none }