如何为TableView创建NSIndexPath

时间:2010-01-27 23:38:00

标签: ios iphone uitableview nsindexpath

我需要在我定义的函数中删除表的第1行。要使用deleteRowAtIndexPath,您必须使用IndexPath并定义部分和行。如何创建这样的索引路径?

以int {1}作为唯一成员的数组将崩溃; NSLog消息表明该部分也需要定义。

*编辑 - >与细胞删除有关的代码:

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

4 个答案:

答案 0 :(得分:633)

使用[NSIndexPath indexPathForRow:inSection:]快速创建索引路径。

编辑:在Swift 3中:

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

答案 1 :(得分:117)

indexPathForRow是一种类方法!

代码应为:

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

答案 2 :(得分:18)

Swift中的强制性答案:NSIndexPath(forRow:row, inSection: section)

您会注意到NSIndexPath.indexPathForRow(row, inSection: section)在swift中不可用,您必须使用第一种方法来构造indexPath。

答案 3 :(得分:17)

对于Swift 3,它现在是:DECLARE @IdProcesso VARCHAR(20) IF(NOT OBJECT_ID('tempdb..#PROCESSOS_PENDENTE') IS NULL) BEGIN DROP TABLE #PROCESSOS_PENDENTE; END SELECT IdProcesso as IdProcesso, ROW_NUMBER() OVER(Order by IdProcesso) AS RowNo INTO #PROCESSOS_PENDENTE FROM fornecedores.aceite WHERE FlgEmProcesso = 0 --PERCORRE TODOS OS REGISTROS E ENVIA OS EMAILS DECLARE @Index INT=1,@TotalCount INT=0 SELECT @TotalCount=COUNT(*) FROM #PROCESSOS_PENDENTE WHILE @Index<=@TotalCount BEGIN SET @IdProcesso='' SELECT @IdProcesso=IdProcesso FROM #PROCESSOS_PENDENTE WHERE RowNo=@Index UPDATE fornecedores.aceite set FlgEmProcesso = 1 WHERE IdProcesso = @IdProcesso SET @Index=@Index+1 END