Reputation: 1733
I am using the UITableviewCell
to create the profile form in my application project. So for this I am using a single cell. The custom cell consists of the UITextField
. Initially these fields are populated with the web service.
Now I can edit the full name as shown below:
You can see I have typed raul in the full name field.
But if I scroll the table view upwards and scroll down back, I lose my changed value and it shows the old entry in the first text field.
I am using the following code:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// create a new cell if needed or reuse an old one
let regCell = profileManagement.dequeueReusableCell(withIdentifier:"regFirstCell", for: indexPath)as? RegistrationFieldTableViewCell
regCell?.registrationField.tag = indexPath.row
regCell?.registrationFieldImage.image = fieldImages[indexPath.row]
let nameFields = NSLocalizedString( regFieldsNames[indexPath.row], comment: "")
regCell?.registrationField.placeholder = nameFields
regCell?.registrationField.autocorrectionType = .no
regCell?.registrationField.keyboardType = UIKeyboardType.default
regCell?.showPassword.isHidden = true
//Swith case for giving keypad to textfields
switch indexPath.row {
case 0:
regCell?.registrationField.text = self.name
case 1:
regCell?.registrationField.text = self.emailId
case 2:
regCell?.registrationField.text = self.civilId
regCell?.registrationField.isUserInteractionEnabled = false
case 3:
regCell?.registrationField.text = self.phoneNumber
regCell?.registrationField.isUserInteractionEnabled = false
case 4:
regCell?.registrationField.isSecureTextEntry = true
regCell?.registrationField.text = "abhijith123"
regCell?.showPassword.isHidden = true
regCell?.registrationField.isUserInteractionEnabled = false
default:
print("Something else")
}
return regCell!
}
Upvotes: 0
Views: 1151
Reputation: 2099
This is due to cell reusability.
You need to save your changes in order to bring them up the next time you view the cell. Currently what you are doing is you are changing the text, swiping away (the cell is reused). then swiping back (the cell is reloaded with the cellForRowAt function, thus the value of it is set)
What you need to do is to actually store the values the user is inserting so that when the user scrolls away then back again, we display the saved value.
To do so, store your values in an array, and in let yourlabel.text = yourArray[indexPath.row]
So assuming you have a tableview with 3 rows, each row have a textview.
1- Define an array: valuesArray = ["", "", ""]
2- set textField.tag = indexPath.row
in the cellAtIndex function
3- set textField.text = valuesArray[indexPath.row]
in the cellAtIndex function as well
4- In you didEndEditing Delegate for the textField, set this: valuesArray[textField.tag] = textField.text
And this should work
Hope this helps!
Upvotes: 3
Reputation: 535850
Think about it. Every time we see row 0, you are saying:
regCell?.registrationField.text = self.name
But you never change self.name
to what the user types in the field. So naturally when we show row 0 again, it reverts to the old self.name
.
Upvotes: 1