Reputation: 31
there is a table View Inside VC2 to Modify or add Contact into phone . the problem it is text fields become blank in Sections when table view scrolled . i found a way to fix this Problem But i cant handle it Properly .
Model :
class ContactModel : NSObject {
var identifier : String!
var thumbnailImageData : UIImage?
var givenName : String!
var familyName : String!
var phoneNumbers : [String]!
var emailAddresses : [String]!
override init() {
self.phoneNumbers = []
self.emailAddresses = []
super.init()
}
VC2 :
var contactModel = ContactModel()
@IBOutlet weak var tvInsert: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.section == 0 {
let cell0 = tableView.dequeueReusableCell(withIdentifier: "InsertTableCell0") as! InsertTableCell0
cell0.txtFirstName.text = self.contactModel.givenName
cell0.txtLastName.text = self.contactModel.familyName
return cell0
}else if indexPath.section == 1 {
let cell1 = tableView.dequeueReusableCell(withIdentifier: "InsertTableCell1") as! InsertTableCell1
cell1.btnDelete.addTarget(self, action: #selector(deleteRowDate(_:)), for: .touchUpInside)
cell1.txtPhoneNumber.placeholder = "Phone Number"
cell1.txtPhoneNumber.text = contactModel.phoneNumbers[indexPath.row]
// here i get textField tag
cell1.txtPhoneNumber.tag = indexPath.row
cell1.txtPhoneNumber.delegate = self
return cell1
}else {
let cell2 = tableView.dequeueReusableCell(withIdentifier: "InsertTableCell2") as! InsertTableCell2
cell2.btnEmail.addTarget(self, action: #selector(deleteRowDate(_:)), for: .touchUpInside)
cell2.txtEmail.placeholder = "Email"
cell2.txtEmail.text = contactModel.emailAddresses[indexPath.row]
return cell2
}
}
func textFieldDidEndEditing(_ textField: UITextField) {
let index = IndexPath(row: textField.tag, section: 1)
let cell = tvInsert.cellForRow(at: index) as? InsertTableCell1
let pointInTable = cell?.txtPhoneNumber.convert(textField.bounds.origin, to: self.tvInsert)
let textFieldIndexPath = self.tvInsert.indexPathForRow(at: pointInTable!)
print(textFieldIndexPath)
}
Upvotes: 0
Views: 950
Reputation: 619
Create model of your all field you need and set array as blank as follow.
var dict:[String:Any] = [:]
dict["description"] = ""
arrGetEducationList.append(EducationDetailsArrModel(JSON: dict)!)
self.tblEducation.reloadData()
After in you tableview cell set model data like that
let edu = self.arrGetEducationList[indexPath.row]
cell.txtSchool.text = edu.edu_institute_name
After that create delegate methods for textfield
func textFieldDidEndEditing(_ textField: UITextField) {
let pointInTable = textField.convert(textField.bounds.origin, to: self.tblEducation)
let IndexPath = self.tblEducation.indexPathForRow(at: pointInTable)
if let cell = tblEducation.cellForRow(at: IndexPath!) as? AddEducationCell1 {
if cell.txtSchool == textField{
arrGetEducationList[(IndexPath?.row)!].edu_institute_name = textField.text!
}
}
}
Upvotes: 1
Reputation: 1197
The UITextField
s will be get empty for sure, hence you did not update your contactModel
with the data typed into that UITextField
. Your method textFieldDidEndEditing
looks Error prone, however in the end you need to update all needed properties of contactModel
within that method.
e.g.:
contactModel.givenName = textField.text
Upvotes: 0
Reputation: 61
you must have to set textfield's value in your model also.
in textFieldDidEndEditing
this method.
Upvotes: 0
Reputation: 27221
Your text fields are obviously reused. So, use temporary storage in order to save the temporary content. e.g, you can use
yourTextField.addTarget(self, action: #selector(textFieldDidChanged(textField:)), for: .editingChanged)
and to catch:
@objc func textFieldDidChange(textField: UITextField){
storage[textField.tag] = textField.text
}
let storage = [Int: String]()
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let tag = tagFromIndexPath(indexPath)
textField.text = storage[tag]
}
Upvotes: 0