Reputation: 267
So I'm having an array of phone numbers and I iterate through it, if one from the array is the same as the one in the textField, I will reload the table row and show some image with number ok and stuff like that. Else let's say if the user now changes that number which was good, like deletes a decimal, now the number no longer matching the ones from the array so I need to change that cell view.But this current code reloading my table for every change in the textField and by reloading it won't stay anymore in editing mode.
@objc func textFieldValueCahnged(_ textField: UITextField) {
if textField.tag == 0 {
if !shouldValidatePhone {
for phone in self.phoneNumbers {
if phone == textField.text! {
self.phoneNumber = phone
GlobalMainQueue.async {
self.phoneValidated = true
// self.reloadCellForIdentifier("ExtraFieldCell")
self.mainTableView.reloadRows(at: [self.rowindexpath!], with: .fade)
}
break
} else {
GlobalMainQueue.async {
self.phoneValidated = false
self.phoneNumber = textField.text!
self.mainTableView.reloadRows(at: [self.rowindexpath!], with: .fade)
// TODO: IF IT 'S typed a different number that the ones in self.phoneNumbers array we need to update the view with the cell.elementsAreHidden()
}
break
}
}
}
} else {
verifyCode = textField.text!
}
}
the tableView cell:
let cell = tableView.dequeueReusableCell(withIdentifier: "PhoneValidationCell") as! PhoneValidationCell
self.rowindexpath = indexPath
cell.label.text = "\(currentField.label):"
cell.textField.addTarget(self, action: #selector(textFieldValueCahnged(_:)), for: .editingChanged)
cell.sendCodeBtn.addTarget(self, action: #selector(sendCodeButtonPressed(_:)), for: .touchUpInside)
cell.textField.tag = 0
cell.sendCodeBtn.tag = 0
cell.textFieldForVeryfing.addTarget(self, action: #selector(textFieldValueCahnged(_:)), for: .editingChanged)
cell.verifyCodeBtn.addTarget(self, action: #selector(sendCodeButtonPressed(_:)), for: .touchUpInside)
cell.textFieldForVeryfing.tag = 1
cell.verifyCodeBtn.tag = 1
if self.phoneNumbers.isEmpty {
if let phoneNumbers = currentField.userPhones {
self.phoneNumbers = phoneNumbers
}
}
if !phoneValidated && !shouldValidatePhone {
if let value = self.extraFieldsValues[currentField.name] as? String {
for i in phoneNumbers {
if i == value {
cell.phoneVerified()
break
} else {
cell.elementsAreHidden()
}
}
if phoneNumbers.isEmpty {
cell.elementsAreHidden()
}
cell.label.textColor = UIColor.black
cell.textField.text = value
self.phoneNumber = value
} else {
cell.label.textColor = Theme.placeholderColor()
cell.textField.text = ""
}
} else {
// cell.phoneVerified() /// check here
cell.textField.text = self.phoneNumber
}
if shouldValidatePhone && !phoneValidated {
cell.phoneToBeVerified()
}
if phoneValidated && shouldValidatePhone {
cell.phoneVerified()
}
basically when phoe number is in the textField I'm showing cell.elementsAreHidden()
. when number needs to be validated cell.phoneToBeVerified()
. and when it s verified cell.phoneVerified()
Upvotes: 0
Views: 67
Reputation: 1910
You should store the indexPath
of the cell you are editing and focus again on it's textfield after the reload by calling cell.textField.becomeFirstResponder()
.
For example:
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if indexPath == self.rowindexpath {
(cell as? ProjectTableViewCell)?.textField.becomeFirstResponder()
}
}
Upvotes: 1