Come creare NSIndexPath per TableView


243

Ho bisogno di eliminare la riga 1 di una tabella in una funzione che ho definito. Per poter utilizzare deleteRowAtIndexPathè necessario utilizzare un IndexPathcon una sezione e una riga definite. Come posso creare un percorso indice come questo?

Un array con int {1} come unico membro andrà in crash; il messaggio NSLog afferma che anche la sezione deve essere definita.

* Modifica -> Codice relativo all'eliminazione delle celle:

    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];

Risposte:


646

Utilizzare [NSIndexPath indexPathForRow:inSection:]per creare rapidamente un percorso di indice.

Modifica: in Swift 3:

let indexPath = IndexPath(row: rowIndex, section: sectionIndex)

Swift 5

IndexPath(row: 0, section: 0)

118

indexPathForRow è un metodo di classe!

Il codice dovrebbe contenere:

NSIndexPath *myIP = [NSIndexPath indexPathForRow:0 inSection:0] ;

18

Risposta obbligatoria in Swift: NSIndexPath(forRow:row, inSection: section)

Noterai che NSIndexPath.indexPathForRow(row, inSection: section)non è disponibile in swift e devi usare il primo metodo per costruire indexPath.


17

Per Swift 3 è ora: IndexPath(row: rowIndex, section: sectionIndex)

Utilizzando il nostro sito, riconosci di aver letto e compreso le nostre Informativa sui cookie e Informativa sulla privacy.
Licensed under cc by-sa 3.0 with attribution required.