mehran kml
mehran kml

Reputation: 31

text field become blank when table View Scrolled in two Section Swift

here is a table View with two Different Section 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 in section 1 but i cant Handle section 2 .

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]
            cell1.txtPhoneNumber.delegate = self
            cell1.txtPhoneNumber.tag = indexPath.row
            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]
            cell2.txtEmail.delegate = self
            cell2.txtEmail.tag = indexPath.row
            return cell2

        }
    }


    func textFieldDidEndEditing(_ textField: UITextField) {

        if let aText = textField.text {
            self.contactModel.phoneNumbers[textField.tag] = aText
        }
   }

Upvotes: 0

Views: 203

Answers (1)

OMEESH SHARMA
OMEESH SHARMA

Reputation: 141

Seems like all the fields delegates are self and textFieldDidEndEditing is setting data for all the fields instead of phone number fields.

you have to check if the textField is phone number or not and also you should update the data in model when textField text is changed instead of end editing.

Easier solution will be :- Remove this line "cell1.txtPhoneNumber.delegate = self"

Replace var phoneNumbers: [String]! in model with following

 var phoneNumbers = ["","",""] //for 3 phone numbers

Put this code in cellForRow of the particular cell i.e (indexPath.section == 1) for the above question

//Saved Phone number in model
cell.txtPhoneNumber.text = contactModel.phoneNumbers[indexPath.row]

//Get and save Phone number when changed
cell.txtPhoneNumber.tag = indexPath.row
cell.txtPhoneNumber.addTarget(self, action: #selector(self.phoneNumberChanged(_:)), for: .editingChanged)

In the ViewController

@objc func phoneNumberChanged(_ sender: UITextField){
   //Phone number changed
   contactModel.phoneNumbers[sender.tag] = sender.text ?? ""
}

In case you want to save the phone number on end Editing replace .editingChanged with .editingDidEnd

Upvotes: 1

Related Questions