>我有一个UITextField
在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
}
如何将相同的文本字段传递给:
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
功能。
您需要在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
}