我有一个带有静态单元格和节标题的 TableViewController。我试图打电话给indexPath.row
但是,它们并不是唯一的。例如
TableView:
- Header 1
- Cell1 indexPath = 0
- Header 2
- Cell2 indexPath = 0
- Cell3 indexPath = 1
- Cell4 indexPath = 2
获取行的唯一标识符的正确方法是什么?
因为使用它会使 2 个单元格具有相同的索引路径
override func tableView(tableView: UITableView, willSelectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath? {
print(indexPath.row)
if indexPath.row != 0 {
return indexPath
}
return nil
}
你似乎忽略了indexPath.section
.节和行的组合唯一地定义索引路径。行在其部分中是唯一的。
NSIndexPath
由row
和section
组成,section
和row
的组合是唯一的您的案例中的单元格 1 具有indexPath.section == 0
和indexPath.row == 0
单元格 2 具有indexPath.section == 1
和indexPath.row == 0