Como criar nsindexpath para o TableView
-
23-09-2019 - |
Pergunta
Preciso excluir a linha 1 de uma tabela em uma função que defini. Para ser usado deleteRowAtIndexPath
você deve usar um IndexPath
com uma seção e linha definidas. Como posso criar um IndexPath como este?
Uma matriz com o int {1} como seu único membro falhará; A mensagem NSLog afirma que a seção também precisa ser definida.
*Editar -> Código relacionado à Cell Excluir:
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];
Solução
Usar [NSIndexPath indexPathForRow:inSection:]
Para criar rapidamente um caminho de índice.
Editar: Em Swift 3:
let indexPath = IndexPath(row: rowIndex, section: sectionIndex)
Outras dicas
indexPathForRow
é um método de classe!
O código deve ler:
NSIndexPath *myIP = [NSIndexPath indexPathForRow:0 inSection:0] ;
Resposta obrigatória no Swift: NSIndexPath(forRow:row, inSection: section)
Você vai notar isso NSIndexPath.indexPathForRow(row, inSection: section)
não está disponível no Swift e você deve usar o primeiro método para construir o IndexPath.
Para Swift 3, agora é: IndexPath(row: rowIndex, section: sectionIndex)