如何在iOS中为TableView创建NSIndexPath?

索引路径通常是一组两个值的集合,分别代表表视图的行和部分。索引路径可以在目标C和Swift中创建,因为两者都是iOS开发的本地语言。

IndexPathForRow是iOS中的类方法。要创建索引路径,我们需要确定需要创建的节和行。以下是创建索引路径的方法。

我们可以使用在目标C中创建IndexPath。

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

NSIndexPath *myIP = [NSIndexPath indexPathForRow: 5 inSection: 2] ;

要在Swift中创建IndexPath,我们可以使用。

IndexPath(row: rowIndex, section: sectionIndex)

IndexPath(row: 2, section: 4)

这两种方法通常都在“单元格行”方法中使用,可以用作

let cell = tblView.cellForRow(at: IndexPath(row: 5, section: 2)