iosswiftuitableviewbecomefirstresponder

Multiple textFields in UITableView update the wrong textField when textFieldShouldReturn


I'm doing a form through a UITableView, using custom UITableViewCells which contain a UITextField each.

I'm using textFieldShouldReturn to jump to the next textField but I cannot understand why what I typed in one textField appears randomly (actually, there is weird pattern) into another textField.

Here the custom UITableViewCell

class RPTableViewCell: UITableViewCell {
    @IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var textField: DictionaryTextField!
    @IBOutlet weak var errorLabel: UILabel!

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        titleLabel.textColor = Constants.secondaryColor
        textField.textColor = Constants.secondaryColor
        contentView.isUserInteractionEnabled = false
        errorLabel.isHidden = true
    }

    func setTag(tag: Int) {
        textField.tag = 100 + tag
    }
}

Then in my FormViewController I do

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    guard let field = formFields?[indexPath.row] else { return UITableViewCell() }        
    if let cell = tableView.dequeueReusableCell(withIdentifier: "cellID") as? RPTableViewCell {
        cell.titleLabel.text = field["displayText"]?.uppercased
        cell.textField.text = field["userAnswer"] as? String // This was missing, but is key
        cell.textField.placeholder = field["placeholder"] as? String
        cell.setTag(tag: indexPath.row)
        cell.textField.delegate = self
        return cell
    } else {
        return UITableViewCell()
    }
}

 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        guard let cell = tableView.cellForRow(at: indexPath) as? RPTableViewCell else { return }
        tableView.scrollToRow(at: indexPath, at: .middle, animated: true)
        cell.textField.becomeFirstResponder()
    }

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        if let nextTextField = tableView.viewWithTag(textField.tag + 1) as? UITextField {
            tableView.deselectRow(at: IndexPath(row: textField.tag - 100, section: 0), animated: false)
            tableView.selectRow(at: IndexPath(row: textField.tag - 99, section: 0),
                                animated: false, scrollPosition: .middle)
            nextTextField.becomeFirstResponder()
        } else {
            textField.resignFirstResponder()
        }
        return false
    }

EDIT:

In viewDidLoad I load the formFiels like this

// Read from a JSON file
guard let visitorPaths = Constants.configDict()?["visitorPaths"] as? [Dictionary<String, AnyObject>] else {
        print("Error: no visitorPaths found in the configuration")
        return
    }
formFields = visitorPaths.first!["fields"]! as? [[String: AnyObject]]

Solution

  • Your are using the following snippet which does not work:

    if let nextTextField = tableView.viewWithTag(textField.tag + 1) as? UITextField 
    

    The textfield is not a subview of your tableView. The Textfield is a subview of the TableViewCell.

    You can acces the cell in the textFieldShouldReturn(_ textField: UITextField) delegate method like the following:

    let nextIndexPath = IndexPath(row: textField.tag + 1, section: 0)
    
    if let cell = tableView.cellForRow(at: nextIndexPath) as? RPTableViewCell {
        cell.textField.becomeFirstResponder()
    }
    

    Edit for the text jumping: Add the following textField delegate method to store the new text:

    func textFieldDidEndEditing(_ textField: UITextField) {
        formFields?[textField.tag - 100]["displayText"] = textField.text
    }