使用TPKeyboardAvoidingTableView时如何访问tableview单元格中的文本字段值

时间:2018-02-04 17:26:56

标签: ios swift uitextfield tableview

我已经集成了TPKeyboardAvoidingTableView来处理文本字段的下一个响应者。如何访问文本字段值,因为我无法分配文本字段代理。

func textFieldDidEndEditing(_ textField: UITextField) {
        if textField.tag == UserDetails.Name.rawValue{
            name = textField.text!
            print(name)
        }
        else if textField.tag == UserDetails.Mobile.rawValue{
            mobile = textField.text!
            print(mobile)
        }
        else if textField.tag == UserDetails.Qualification.rawValue{
            qualification = textField.text!
            print(qualification)
        }
        else if textField.tag == UserDetails.Area.rawValue{
            area = textField.text!
            print(area)
        }
    }

1 个答案:

答案 0 :(得分:0)

不是最好的方法,但应该有效:

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    var indexPath: IndexPath?
    if (textField.superview?.superview is YourCell) {
        let cell = textField.superview?.superview as? UITableViewCell
        indexPath = tableView.indexPath(for: cell)
    }
    let indexPathNextRow = IndexPath(row: indexPath?.row + 1, section: indexPath?.section ?? 0)
    let cellNextRow = (tableView.cellForRow(at: indexPathNextRow) as? YourCell) as? YourCell
    let tag = Int(indexPathNextRow.row)
    let textFieldNextRow = cellNextRow?.contentView.viewWithTag(tag) as? UITextField
    textFieldNextRow?.becomeFirstResponder()
    print("last text field---------->\(Int(textField.tag))")
    return true
}

func textFieldDidEndEditing(_ textField: UITextField) {
    textValues[textField.tag] = textField.text ?? ""
}  

不要忘记为单元格的UITextField

设置标记