I’ve a TableView with TextField for each row. The TableView is composed by two section.
When the user tap on return button of KeyBoard, I set the text of TextField on NSUserDefault.
In the cellForRowAtIndexPath method i read the value of text on NSUserDefault.
Why when I scroll the TableView, the data changes?
On cellForRowAtIndexPath:
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
//set cell value from NSArray
if ((indexPath.section) == 0) {
cell.textLabel.text = [sicurezzaSullaStrada objectAtIndex:indexPath.row];
} else {
cell.textLabel.text = [infoVeicolo objectAtIndex:indexPath.row];
}
//set TextField value from NSUserDefault
if (indexPath.section == 0) {
//first section
if (indexPath.row == 0) {
TextFieldCell = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 80, 21)];
TextFieldCell.adjustsFontSizeToFitWidth = YES;
TextFieldCell.placeholder = @"Enter text";
TextFieldCell.delegate = self;
[TextFieldCell setText:[[NSUserDefaults standardUserDefaults] objectForKey:@"stringPesoKg"]];
cell.accessoryView = TextFieldCell;
}
if (indexPath.row == 1) {
TextFieldCell = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 80, 21)];
TextFieldCell.adjustsFontSizeToFitWidth = YES;
TextFieldCell.placeholder = @"Enter Text";
TextFieldCell.delegate = self;
[TextFieldCell setText:[[NSUserDefaults standardUserDefaults] objectForKey:@"stringPostiLettoOmologati"]];
cell.accessoryView = TextFieldCell;
} else {
//second section
if (indexPath.row == 0) {
//Riga relativa alla Data di immatricolazione
TextFieldCell = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 80, 21)];
TextFieldCell.adjustsFontSizeToFitWidth = YES;
TextFieldCell.placeholder = @"Enter Text";
TextFieldCell.delegate = self;
[TextFieldCell setText:[[NSUserDefaults standardUserDefaults] objectForKey:@"stringDataImmatricolazione"]];
cell.accessoryView = TextFieldCell;
}
And then ontextFieldDidEndEditing:
if (posizSect == 0) {
if (posizRow == 0) {
NSUserDefaults *stringDefaultPesoKg = [NSUserDefaults standardUserDefaults];
[stringDefaultPesoKg setObject:textField.text forKey:@"stringPesoKg"];
}
if (posizRow == 1) {
NSUserDefaults *stringDefaultPostiLettoOmologati = [NSUserDefaults standardUserDefaults];
[stringDefaultPostiLettoOmologati setObject:textField.text forKey:@"stringPostiLettoOmologati"];
}
if (posizRow == 2) {
NSUserDefaults *stringDefaultPostiLettoOmologati = [NSUserDefaults standardUserDefaults];
[stringDefaultPostiLettoOmologati setObject:textField.text forKey:@"stringLunghezzaMax"];
} else {
if (posizRow == 0) {
NSUserDefaults *stringDefaultPesoKg = [NSUserDefaults standardUserDefaults];
[stringDefaultPesoKg setObject:textField.text forKey:@"stringDataImmatricolazione"];
}
On didSelectRowAtIndexPath:, I set the value of PosizRow and PosizSect:
posizRow = indexPath.row;
posizSect = indexPath.section;
Thanks,
Alessandro from Italy
The reason that the data changes, is that the UITableViewCells get dequeued when you scroll them off the screen. The data is then gone at this point, and when you scroll back, the cell is repopulated with the initial values (not the changed ones). I don’t know how to fix it, I am looking for that solution now. When I find it, I’ll link you to it.
*EDIT***
Ok here is what you do:
First, you need to make your ViewController a delegate for UITextFieldDelegate. Then you need to implement:
In my case, the user interface is done dynamically in code, so I Have an NSMutableDictionary that contains the names of all of the fields. So in my code I do the following:
If you have your text fields as properties then you’ll need another way to update your storage variable. For instance, you might use a tag on the textField to identify which field.