senty senty - 7 months ago 51
Swift Question

IndexPath.row is not unique for Static Cells

I have a TableViewController with static cells and section headers. I tried to call

indexPath.row
however, they are not unique.. For example

TableView:
- Header 1
- Cell1 indexPath = 0
- Header 2
- Cell2 indexPath = 0
- Cell3 indexPath = 1
- Cell4 indexPath = 2


What is the proper way of getting unique identifiers for rows?




Because using this make 2 cells have the same indexPath

override func tableView(tableView: UITableView, willSelectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath? {
print(indexPath.row)

if indexPath.row != 0 {
return indexPath
}

return nil
}

Answer

You seem to be ignoring indexPath.section. The combination of section and row uniquely define the index path. A row is unique within its section.

Comments