2010-05-09 31 views
46

Ai-je raison de penser que pour changer la coche pour « sur » « off », je dois changer la CellAccessoryType entre none et checkmark sur le didSelectRowAtIndexPath?UITableViewCell changement coche sur certains

Parce que j'ai fait cela mais j'ai remarqué que le comportement n'est pas parfaitement identique à celui des cellules de contrôle sur les réglages de verrouillage automatique sur l'iPhone.

Ou y a-t-il une autre manière de cocher?

Répondre

70
  1. Gardez une propriété dans votre contrôleur de vue appelé selectedRow, qui représente l'indice d'une ligne qui représente l'élément coché dans une section de table.

  2. Dans -tableView:cellForRowAtIndexPath: méthode de délégué de votre contrôleur de vue, définissez la accessoryType du cell-UITableViewCellAccessoryCheckmark si indexPath.row de la cellule est égale à la valeur selectedRow. Sinon, réglez-le sur UITableViewCellAccessoryNone.

  3. Selon vous méthode de délégué du contrôleur -tableView:didSelectRowAtIndexPath:, définissez la valeur selectedRow à l'indexPath.row sélectionné, par exemple: self.selectedRow = indexPath.row

+0

Désolé, je ne veut pas dire que j'avais plusieurs cellules coches (plus comme des boutons radio). Juste ils n'ont pas animé aussi doucement (mais j'ai arrangé cela maintenant). Mais votre réponse signifie que j'utilise les méthodes correctes :) –

+7

Pour ajouter à la solution d'Alex - pour faire apparaître la coche lorsque vous appuyez sur la cellule, vous devez également mettre [tableview reloadData] après avoir défini la cellule sélectionnée. –

+6

C'est facile, mais il y a peut-être un problème de performance lié au rechargement des données dans toute la table. Parce que vous savez quel 'indexPath.row' vous devez mettre à jour, vous avez seulement besoin de mettre à jour cette ligne, ce qui peut être fait avec la méthode de vue table' -reloadRowsAtIndexPaths: withRowAnimation: 'Pour plus de détails: http://developer.apple. com/bibliothèque/ios/documentation/UIKit/Référence/UITableView_Class/Référence/Reference.html #/apple_ref/doc/uid/TP40006943-CH3-SW40 –

3

Il devrait être

didHighlightRowAtIndexPath

au lieu de

tableView: didSelectRowAtIndexPath

3

réponse Alex a travaillé pour moi seulement après ajout de la table de recharge, dans .h

{ 
    int selectedCell; 
} 
@property(nonatomic,readwrite)int selectedCell; 

dans .m * cellForRowAtIndexPath *

if(indexPath.row == selectedCell) 
    { 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 
     cell.selected = YES; 
    } 
    else 
    { 
     cell.accessoryType = UITableViewCellAccessoryNone; 
     cell.selected = NO; 
    } 

et partout didHighlightRowAtIndexPath ou didSelectRowAtIndexPath

self.selectedCell = indexPath.row; 
    [self.tableView reloadData]; 
56

Une autre solution:

-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    NSIndexPath *oldIndex = [self.tableView indexPathForSelectedRow]; 
    [self.tableView cellForRowAtIndexPath:oldIndex].accessoryType = UITableViewCellAccessoryNone; 
    [self.tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark; 
    return indexPath; 
} 

Et oui: vous n'avez pas vérifier si oldIndex est nil :)


Swift 3 et plus tard:

override func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? { 
    if let oldIndex = tableView.indexPathForSelectedRow { 
     tableView.cellForRow(at: oldIndex)?.accessoryType = .none 
    } 
    tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark 

    return indexPath 
} 
+0

Bonne réponse! +1 – BigSauce

+1

+1 pour une réponse courte –

+1

C'est vraiment un bon code. J'ai ajouté la version Swift. –

4

Zyphrax a proposé une excellente solution qui fonctionnait très bien pour moi!Et si vous devez effacer la précédente ligne sélectionnée, il suffit d'utiliser:

[self.tableView reloadData]; 

dans

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
0

Si vous souhaitez utiliser votre image personnalisée comme accessoryType utilisation ci-dessous le code

-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    UIImageView *imgCheckMark = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"select_icon.png"]]; 
    [imgCheckMark setFrame:CGRectMake(self.view.frame.size.width - 30, 25, 14, 18)]; 
    imgCheckMark.tag = 1000+indexPath.row; 

    NSIndexPath *oldIndex = [self.tblSelectService indexPathForSelectedRow]; 
    [self.tblSelectService cellForRowAtIndexPath:oldIndex].accessoryView = UITableViewCellAccessoryNone; 
    [self.tblSelectService cellForRowAtIndexPath:indexPath].accessoryView = imgCheckMark; 
    return indexPath; 
} 
2

swift:

var selectedCell:UITableViewCell?{ 
    willSet{ 
     selectedCell?.accessoryType = .None 
     newValue?.accessoryType = .Checkmark 
    } 
} 

alors:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    selectedCell = self.tableView.cellForRowAtIndexPath(indexPath) 
    self.mapView.selectAnnotation(self.mapView.annotations[indexPath.row], animated: true) 
} 
+1

Grande solution ... –

+0

Excellente approche du problème. Je vous remercie! – oyalhi