繁体   English   中英

从UITableViewCell中的UITextField访问文本

[英]Access text from UITextField in a UITableViewCell

我正在实现一个UITableViewController,其中前两个包含3个部分,分别包含2和1个文本字段。 我在访问文本字段中输入的文本时遇到问题。 我不知道如何在iOS Swift中解决此问题,请帮忙。

在表视图控制器中

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let gender = Gender(rawValue: indexPath.row)

        switch indexPath.section {
        case 0:
            guard let nameCell = tableView.dequeueReusableCellWithIdentifier("addNameCellID") as? ChildFormViewCell else {
                return UITableViewCell()
            }
            nameCell.firstNameField.text = "" // just in case cells are re-used, this clears the old value
            nameCell.firstNameField.tag = indexPath.row
            return nameCell

        case 1:
            self.tableView.rowHeight = 50
            guard let dateCell = tableView.dequeueReusableCellWithIdentifier("addDateCellID") as? ChildFormViewCell else {
                return UITableViewCell()
            }
            return dateCell

        case 2:
            guard let genderCell = tableView.dequeueReusableCellWithIdentifier("addGenderCellID") as? ChildFormViewCell else {
                return UITableViewCell()
            }
            genderCell.genderLabel.text = gender?.nameString()
            return genderCell

        default:
        return UITableViewCell()
        }
    }

extension AddChildFormTVC: ChildFormViewCellDelegate {
    func getFirstName(text: String?) {
        print(text)
    }
    func getLastName(text: String?) {
        print(text)
    }
    func getDateOfBirth(text: String?) {
        print(text)
    }
    func getGender(text: String?) {
        print(text)
    }

}

class ChildFormViewCell: UITableViewCell {

    @IBOutlet weak var firstNameField: UITextField!
    @IBOutlet var lastNameField: UITextField!
    @IBOutlet var dateField: UITextField!
    @IBOutlet var genderLabel: UILabel!
    var delegate: ChildFormViewCellDelegate!


}

extension ChildFormViewCell: UITextFieldDelegate {
    func textFieldDidEndEditing(textField: UITextField) {
        switch(textField) {
        case self.firstNameField:
            self.delegate.getFirstName(firstNameField.text)

        case self.lastNameField:
            self.delegate.getLastName(lastNameField.text)

        case self.dateField:
            self.delegate.getDateOfBirth(dateField.text)

        case self.genderLabel:
            self.delegate.getGender(genderLabel.text)

        default:
            return
        }
    }
}

protocol ChildFormViewCellDelegate {
    func getFirstName(text: String?)
    func getLastName(text: String?)
    func getDateOfBirth(text: String?)
    func getGender(text: String?)
}

然后尝试:

    //This will call back after focus is lost from the input    
self.textField.addTarget(self, action: #selector(updateFirstName), forControlEvents: .EditingDidEnd)
    //This will call back with every change, i.e. each letter
self.textField.addTarget(self, action: #selector(updateFirstName), forControlEvents: .EditingChanged)

此外,在将self添加为目标的情况下, self表示UITableViewController 据我所知,对象不能是它自己的委托。 如果那是可能的,那将毫无意义。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM