问题
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