I have a custom UITableViewCell Class.
The cell has editable UITextField. when this textfield is edited I trap the end of edit inside
func textFieldDidEndEditing(_ textField: UITextField)
my custom cell class implements UITextFieldDelegate so I do indeed get the call back.
Similar to all the other responses to similar questions - I first tried to just set the tag on the UITextField to the row number when loading the table.
This works - BUT - now there is a wrinkle .... My table allows the rows to be moved. When the row is moved it retains the tag from when the table was loaded so not my tags are all off from the insert on down. I could solve this by reloading the table after a move - but that jumps the list back to the top.
1) is there a cleaner way to get the row number inside the custom cell class without using .tag? because this fails when I allow rows to be moved.
2) if no to #1 then is there a way to go ahead and reload the display for first grab the current top displayed row and then after table reload reset to the same top current row so the users does not jump back to the top?
I also tried to just use this
func tableView(_ tableView: UITableView, didEndEditingRowAt indexPath: IndexPath?)
and trap the end of editing inside the UIViewController that contains the UITableView so I would have the row number after the edit BUT this never gets called. so the other way to solve this might be to force didEndEditingRowAt to get called. For some reason this wont get called. even if I tuck in to the customer cell class the request to force end of editing
var textFieldHandle : UITextField?
func textFieldDidBeginEditing(_ textField: UITextField) {
print("editing field in cell")
textFieldHandle = textField
}
func textFieldDidEndEditing(_ textField: UITextField) {
print("done editing text field in cell with text -\(textField.text!)-")
if textFieldHandle != nil {
textFieldHandle?.endEditing(true) // force end editing so get table event - does not work
if let index = textFieldHandle?.tag {
if index >= 0 && index < quizListItems.count {
// if we have a proper index then store back in answer array so we dont lose it
quizListItems[index].text = textField.text!
print("--set value at index \(index)")
}
}
}
}