Reputation: 477
I have a UITableView with 6 identical cells. In the cells are UITextFields where a user can type their favorite TV shows. I have figured out a way to record these values with the code listed bellow, but I have one major problem, which is if a user enters a value in one of the fields, finishes editing, but then clicks back on that field and changes the value. I would like to only record one value for each cell. With the current way I save the values, if a user enters a value, finishes editing that text field and then changes the info, it records both values, not just the final one.
Here is an example: A user enters their first favorite, Elementary, and then continues filling in the next five cells. They then decide to go back and change their first choice in the first cell to True Detective. When the user clicks done, it should save True Detective and then the rest of the values. With the way I currently have it set up, it would save Elementary, True Detective and then the rest of the values. How would I fix this.
The code I am currently using:
- (void) textFieldDidEndEditing:(UITextField *)textField {
NSString *nameString = [[NSString alloc] init];
nameString = textField.text;
[self.favoriteShows addObject:nameString];
}
Upvotes: 0
Views: 294
Reputation: 5448
Instead of adding the strings in the textFieldDidEndEditing
method, add them on the done button's click method.
Here are some steps you could try in the done button's method:
Iterate through the tableViewCell
s, get the cell by cellForRowAtIndexPath
.
To obtain the reference of the textField
, use [cell viewWithTag:yourtag];
.
Then add the text by [self.favoriteShows addObject:textField.text];
This way, only the final values will be recorded, which are present in the textField
s at the time when user presses the done button. Hope this helps.
Upvotes: 1