How to call textField that is a inside of UITableViewCell (swift)

浪子不回头ぞ 提交于 2019-12-14 04:21:48

问题


I have a UITextField that is inside of UITableViewCell:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! TextFieldTableViewCell
    let textField: UITextField = cell.textField
    let detail = self.detailItem

    textField.text = detail!.valueForKey("name")!.description + detail!.valueForKey("number")!.description
    print()
    return cell
}

How can pass that same textField to:

func textFieldDidBeginEditing(textField: UITextField) {
    print("executed")

    let detail = self.detailItem
    let textField: UITextField = self.text.textField
    if (textField.text != detail!.valueForKey("name")!.description + detail!.valueForKey("number")!.description) {
        detail?.setValue(textField.text, forKey: "name")
    }
}

Solved

I needed to add textField.delegate = self after its definition.

I assume it connects the view to the view controller, allowing it to see the textFieldDidStopEditing function.


回答1:


you need to give UITextFieldDelegate in cellForRowAtIndexPath Method itself

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! TextFieldTableViewCell
    let textField: UITextField = cell.textField

    textField.delegate = self

    let detail = self.detailItem

    textField.text = detail!.valueForKey("name")!.description + detail!.valueForKey("number")!.description

    return cell

}


来源:https://stackoverflow.com/questions/50346771/how-to-call-textfield-that-is-a-inside-of-uitableviewcell-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!