Come creare NSIndexPath per TableView
-
23-09-2019 - |
Domanda
Ho bisogno di cancellare la riga 1 di una tabella in una funzione che ho definito. Per poter utilizzare deleteRowAtIndexPath
è necessario utilizzare un IndexPath
con una sezione e fila definito. Come posso creare un indexpath come questo?
Un array con l'int {1} come suo unico membro andrà in crash; il messaggio NSLog afferma che la sezione deve essere definito come bene.
* Modifica -> Codice in materia di cellule eliminare:
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];
Soluzione
Usa [NSIndexPath indexPathForRow:inSection:]
per creare rapidamente un percorso di indice.
Modifica In Swift 3:
let indexPath = IndexPath(row: rowIndex, section: sectionIndex)
Altri suggerimenti
indexPathForRow
è un metodo della classe!
Il codice dovrebbe leggere:
NSIndexPath *myIP = [NSIndexPath indexPathForRow:0 inSection:0] ;
Risposta obbligatorio in Swift: NSIndexPath(forRow:row, inSection: section)
Si noterà che NSIndexPath.indexPathForRow(row, inSection: section)
non è disponibile in rapida e si deve utilizzare il primo metodo per costruire l'indexPath.
Per Swift 3 E 'ora: IndexPath(row: rowIndex, section: sectionIndex)