我在 tableView 中有一部分行(第 1 部分),我需要为该部分中的所有行初始化一个 [NSIndexPath]
数组。
我知道我可以很容易地计算该部分中的行数:
let rowsInSection = tableView.numberOfRowsInSection(1)
但这只会让我得到行数,而不是索引路径。
检索索引路径的最干净、最“类似 Swift”的方法是什么?
我正在寻找的示例:
func indexPathsForRowsInSection(section: Int) -> [NSIndexPath] {
// magic happens here
return indexPathsForRowsInSection
}
最佳答案
在 Swift 3 中就像这样:
func indexPathsForRowsInSection(_ section: Int, numberOfRows: Int) -> [NSIndexPath] {
return (0..<numberOfRows).map{NSIndexPath(row: $0, section: section)}
}
关于ios - 在 Swift 的 UITableView 部分获取行的 [NSIndexPath] 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35787523/