如何调用 UITableViewCell 内部的 textField (swift) [英] How to call textField that is a inside of UITableViewCell (swift)

查看:46
本文介绍了如何调用 UITableViewCell 内部的 textField (swift)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 UITableViewCell 中有一个 UITextField:

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
}

如何将相同的 textField 传递给:

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")
    }
}


已解决

我需要在其定义之后添加 textField.delegate = self.

我假设它将视图连接到视图控制器,允许它看到 textFieldDidStopEditing 函数.

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

推荐答案

你需要在 cellForRowAtIndexPath 方法中给 UITextFieldDelegate 自己

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

}

这篇关于如何调用 UITableViewCell 内部的 textField (swift)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆