Comment créer NSIndexPath pour TableView
-
23-09-2019 - |
Question
Je dois supprimer la ligne 1 d'une table dans une fonction que je l'ai défini. Pour utiliser deleteRowAtIndexPath
vous devez utiliser un IndexPath
avec une section et la ligne définie. Comment puis-je créer un indexPath comme ça?
Un tableau avec l'int {1} comme seul membre se bloque; le message NSLog indique que la section doit être définie ainsi.
* Edition -> Code relatives à la cellule supprimer:
NSIndexPath *myIP = [[NSIndexPath alloc] indexPathForRow:0 inSection:0];
NSArray *myArray = [[NSArray alloc] initWithObjects:myIP, nil];
// [self.tableView beginUpdates];
[self.tableView deleteRowsAtIndexPaths:myArray withRowAnimation:UITableViewRowAnimationFade];
// [self.tableView endUpdates];
La solution
Utilisez [NSIndexPath indexPathForRow:inSection:]
pour créer rapidement un chemin d'index.
Modifier Dans Swift 3:
let indexPath = IndexPath(row: rowIndex, section: sectionIndex)
Autres conseils
indexPathForRow
est une méthode de classe!
Le code doit se lire:
NSIndexPath *myIP = [NSIndexPath indexPathForRow:0 inSection:0] ;
réponse obligatoire à Swift: NSIndexPath(forRow:row, inSection: section)
Vous remarquerez que NSIndexPath.indexPathForRow(row, inSection: section)
est pas disponible dans rapide et vous devez utiliser la première méthode pour construire le indexPath.
Pour Swift 3 il est maintenant: IndexPath(row: rowIndex, section: sectionIndex)