ios - 在 Swift 的 UITableView 部分获取行的 [NSIndexPath] 数组

标签 ios swift uitableview

我在 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/

相关文章:

ios - 在 iOS swift 中实现完成时出错

ios - 如何从代码更改我的自定义设置?

swift - CIFaceFeature 只检测一张脸

ios - NSPathStore2 的 UIImageView 实例泄露

objective-c - Println() 是否在实时应用程序中使用内存?

ios - 让 UIButton 粘在 UITableView 的底部

ios - 多个复选框选择并单独打印选中的行标题

ios - 更新基于 UITableView 高度的 UIScrollView 的 contentSize

ios - 快速解析 CSV 文件

ios - 新的 iTunes 连接界面——是否应该立即在 "Prerelease"上看到它?