首页 > 解决方案 > 编辑(文本字段而非表格)结束时如何跳转到 TableView 的下一个单元格中的 UITextField

问题描述

我正在编写一个具有固定大小表和每个单元格中的几个 UITextFields 的应用程序。用户需要在文本字段中输入数据,我想在当前文本字段的编辑停止后自动跳转到下一个文本字段。

我在 TableViewCell 类中有以下代码(从 stackoverflow 上的其他地方修改),它可以很好地跳转到单元格中的下一个字段,但不会跳转到下一个单元格。

func textFieldDidEndEditing(_ textField: UITextField) {
     // Try to find next responder
    if let nextField = textField.superview?.viewWithTag(textField.tag + 1) as? UITextField {
        nextField.becomeFirstResponder()
    } else {
        textField.resignFirstResponder()
    }
    return
}

我意识到这是因为在第二次编辑之后,下一个文本字段不在同一个单元格中,但我不知道如何处理下一个单元格中的文本字段以使其成为 FirstResponder。我在单元格中的文本字段上有一个观察者来检查更改并更新数组:

@IBAction func updatePs(_ sender: Any) {
    psChanged?(ps.text ?? "0")
}

这是在 UITableView cellForRowAt 函数中的位置:

cell.psChanged = { value in
    self.psP[indexPath.row] = Int(value) ?? 0
}

我虽然使用这个观察者来改变 UITableView cellForRowAt 函数中的 FirstResponder ,但我不知道如何去做。

PS 我知道在表格中使用标签可能会出现问题,但表格是固定大小的,无法删除行。

标签: iosswiftuitableviewfirst-responder

解决方案


推荐阅读