Hi guys,
I don't know if it's something in my code or a bug but for some reason on my latest project tableView.indexPathForRow(at: to:) is returning an index that is off by one.
I have a stripped down tableview controller with a custom cell and button linked to the "testButton" method. I also tagged the button with the indexPath.row from the cellForRowAt method so I know the cell is in the correct place.
The problem is, in tableView.indexPathForRow, the tag shows up with the expected value, but the indexPath.row from that method is off by one.
class TestListViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
configureTableView()
}
}
extension TestListViewController {
private func configureTableView() {
tableView.register(TestCell.self, forCellReuseIdentifier: "TestCell")
}
@objc func testButton(_ sender: UIButton) {
guard let indexPath = tableView.indexPathForRow(at: sender.convert(CGPoint.zero, to: self.tableView)) else {
return
}
print("Tag: \(sender.tag)") //This comes up as expected and the tag matches the row
print("IndexPath: \(indexPath)") //This does not, index row is off by one!
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TestCell", for: indexPath) as! TestCell
cell.nameLabel.text = "Row \(indexPath.row)"
cell.checkbox.addTarget(self, action: #selector(testButton(_ :)), for: .touchUpInside)
cell.checkbox.tag = indexPath.row
return cell
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 50
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
}
In my actual app there is a checkbox on each cell and if I tap the first cells checkbox obviously the app crashes because the index is out of range, if I tap the second cells checkbox, the first cells checkbox fills in.
I'm at an utter loss, I have used this method many times in production apps just fine so I'm hoping it's just something I'm forgetting to do.