Reputation: 160
I'm trying to retrieve text from the UITextField
in custom cell and assign the value to self.email
variable in my view controller. When I make a breakpoint in textFieldDidEndEditing
function, I can see that the value of the textField
is assigned to self.email
as expected, but the variable self.email
is nil in @IBAction doneButtonDidPress
function.
Question: Why the value of self.email
is changed to nil
?
extension ViewController: UITextFieldDelegate {
func textFieldDidEndEditing(_ textField: UITextField) {
if let text = textField.text {
self.email = text
}
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
let cell = self.tableView.cellForRow(at: IndexPath(row: 2, section: 0)) as! AboutCell
cell.textView?.becomeFirstResponder()
return true
}
}
I'm using UITableViewController
, that's how I create cells:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
switch indexPath.row {
case 0:
let cell = tableView.dequeueReusableCell(withIdentifier: "profilePhotoCell")! as! ProfilePhotoCell
cell.delegate = self
return cell
case 1:
let cell = tableView.dequeueReusableCell(withIdentifier: "textFieldCell")! as! TextFieldCell
cell.textField.delegate = self
cell.textLabel?.text = "Email"
return cell
default:
let cell = tableView.dequeueReusableCell(withIdentifier: "aboutCell")! as! AboutCell
cell.textView.delegate = self
return cell
}
}
@IBAction
function:
@IBAction func doneButtonDidPress(_ sender: Any) {
print(self.email) // returns nil
}
Upvotes: 0
Views: 2453
Reputation: 160
The solution is to put self.tableView.endEditing(true)
in @IBAction func doneButtonDidPress(_:)
.
Upvotes: 1
Reputation: 2527
In
cellForRowAtIndexPath
assign set tag to youtextfield
and setUITextfieldDelegate
tooNote this is an example for tableview having to textfield Name and RollNo
var txtName:String = ""
var txtRollNo:String = ""
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let YourCell = tableView.dequeueReusableCellWithIdentifier("YourTableViewCellIdentifier") as! YourTableViewCell
YourCell.txtField?.text = itemsData as? String
YourCell.txtField.delegate = self
YourCell.txtField.tag = (indexPath.section*10)+indexPath.row
if(indexPath.section == 0){
switch indexPath.row {
case 0:
YourCell.txtField.placeholder = "Enter Name "
if (self.txtName != ""){
YourCell.txtField.text = self.txtName
}
break;
case 1:
YourCell.txtField.placeholder = "Enter Rollno "
if (self.txtRollNo != ""){
YourCell.txtField.text = self.txtRollNo
}
break;
}
}
return YourCell
}
func textFieldDidBeginEditing(textField: UITextField) {
print(textField.tag)
if textField.tag == 0 {
self.txtName = textField.text
}else if textField.tag == 1{
self.txtRollNo = textField.text
}
}
func textFieldDidEndEditing(textField: UITextField) {
print(textField.tag)
if textField.tag == 0 {
self.txtName = textField.text
}else if textField.tag == 1{
self.txtRollNo = textField.text
}
}
func printValue(){
print("\(self.txtName)")
print("\(self.txtRollNo)")
}
Hope this might help you.
Upvotes: 0
Reputation: 165
You are not refreshing the datasource of your tableview, in didendediting. Your tableview datasource will be an array. You need to assign the textfield value to the array at the corresponding index
In textFieldDidEndEditing
,
var myCell:UITableViewCell
var indexPath: NSIndexPath
myCell = (textField.superview?.superview as! UITableViewCell)
indexPath = table_view.indexPathForRowAtPoint(myCell.center)!
At this index path row , assign the value of the textfield to your tableview datasource(array)
self.tableview_list.objectAtIndex(indexPath.row).setObject(self.email, forKey: "XYZ")
And also reload the tableview at that particular index only.
Upvotes: 0