gpt4 book ai didi

swift - 如何调用 UITableViewCell 内部的 textField (swift)

转载 作者:行者123 更新时间:2023-11-30 11:30:04 25 4
gpt4 key购买 nike

我有一个位于 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
}

如何将相同的文本字段传递给:

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

我假设它将 View 连接到 View Controller ,允许它查看 textFieldDidStopEditing 函数。

最佳答案

您需要在 cellForRowAtIndexPath 方法本身中给出 UITextFieldDelegate

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

}

关于swift - 如何调用 UITableViewCell 内部的 textField (swift),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50346771/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com