IndexPath.row 对于静态单元格并不是唯一的



我有一个带有静态单元格和节标题的 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.节和行的组合唯一地定义索引路径。行在其部分中是唯一的。

NSIndexPathrowsection组成,sectionrow的组合是唯一的您的案例中的单元格 1 具有indexPath.section == 0indexPath.row == 0单元格 2 具有indexPath.section == 1indexPath.row == 0

相关内容

最新更新