在UITableViewCell中访问UITextField中的文本

时间:2016-04-06 16:13:12

标签: ios swift

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

在表格视图控制器

SELECT
[KEY] 
,STUFF((SELECT distinct ', ' + [Code] FROM #tempTable t WHERE t.[KEY]=tt.[KEY] FOR XML PATH('')),1,1,'') AS [Code] 
,STUFF((SELECT distinct ', ' + CAST([CreatedDate] AS NVARCHAR(100)) FROM #tempTable t WHERE t.[KEY]=tt.[KEY] FOR XML PATH('')),1,1,'') AS [CreatedDate] 
FROM #tempTable tt
Group by KEY 
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)
    }

}

1 个答案:

答案 0 :(得分:0)

然后尝试:

    //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。据我所知,一个对象不能是它自己的委托。如果可能的话,那就没有意义了。