SWIFT – How to get the value of the text field from UITABLEVIEWCELL?

So I have this UITableView cell with 4 UITextFields, and I want to get their values ​​when the button is clicked.

This code does not retrieve any Value.

@IBAction func printBtnAction(_ sender: Any) {

let cell = self.myTableView.dequeueReusableCell(withIdentifier: "manualAddC1") as! AddFriendC1Cell

let alias = cell.aliasTextField.text!
let primaryPhone = cell.primaryPhoneTextField.text!
let secondaryPhone = cell.seondaryPhoneTextField.text!
let email = cell.emailAddressTextField.text!

print("Alias: \(alias), Phone: \(primaryPhone), Phone2: \(secondaryPhone), Email: \(email)")< br />}

This is a screenshot of TableView
enter image description here

I finally found the answer through this simple solution.

@IBAction func saveBtnAction(_ sender: Any) {

let index = IndexPath(row: 0, section: 0)
let cell: AddFriendC1Cell = self.myTableView.cellForRow(at: index) as! AddFriendC1Cell
self.alias = cell.aliasTextField.text!
self.primaryPhone = cell.primaryPhoneTextField.text!
self.secondaryPhone = cell.seondaryPhoneTextField.text!
self.email = cell.emailAddressTextField.text!

print("Alias: \(self.alias), Phone: \(self.primaryPhone), Phone2: \(self.secondaryPhone), Email: \(self.email)")
}

So I have this UITableView unit with 4 UITextField Grid, I want to get their values ​​when the buttons are clicked.

This code does not retrieve any values.

@IBAction func printBtnAction(_ sender: Any) {

let cell = self.myTableView.dequeueReusableCell(withIdentifier: "manualAddC1") as! AddFriendC1Cell

let alias = cell.aliasTextField.text!
let primaryPhone = cell.primaryPhoneTextField.text!
let secondaryPhone = cell.seondaryPhoneTextField.text!
let email = cell.emailAddressTextField.text!

print("Alias: \(alias), Phone: \(prima ryPhone), Phone2: \(secondaryPhone), Email: \(email)")
}

This is a screenshot of TableView
enter image description here

I finally found the answer through this simple solution.

@IBAction func saveBtnAction(_ sender: Any) {

let index = IndexPath(row: 0, section: 0)< br /> let cell: AddFriendC1Cell = self.myTableView.cellForRow(at: index) as! AddFriendC1Cell
self.alias = cell.aliasTextField.text!
self.primaryPhone = cell.primaryPhoneTextField.text!< br /> self.secondaryPhone = cell.seondaryPhoneTextField.text!
self.email = cell.emailAddressTextField.text!

print("Alias: \(self.alias), Phone: \ (self.primaryPhone), Phone2: \(self.secondaryPhone), Email: \(self.email)")
}

Leave a Comment

Your email address will not be published.