This is an extension of this question here: UITextField randomly reusing text in tableview
I'm having some issues with my table that has UITextFields in each cell. I notice text is still getting reused randomly in the wrong cells even though I seem to be saving it correctly.
else if (tableView == self.vitalsTableView)
{
if ([indexPath row] == 2) {
CellIdentifier = @"bloodPressureCell";
BloodPressureTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.textField1.text = [self.vitalsDictionary objectForKey:@"blood_pressure_1"];
cell.textField2.text = [self.vitalsDictionary objectForKey:@"blood_pressure_2"];
self.bloodPressureTextField1 = cell.textField1;
self.bloodPressureTextField2 = cell.textField2;
return cell;
}
else if ([indexPath row] == 9){
CellIdentifier = @"statusCell";
SmokingStatusTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
return cell;
}
else if ([indexPath row] == 10){
CellIdentifier = @"vitalsCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
return cell;
}
else{
CellIdentifier = @"textCell";
VitalsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
switch (indexPath.row) {
case 0:
cell.vitalsLabel.text = @"Temperature";
cell.textField.text = [self.vitalsDictionary objectForKey:@"temperature"];
self.temperatureTextField = cell.textField;
break;
case 1:
cell.vitalsLabel.text = @"Pulse";
cell.textField.text = [self.vitalsDictionary objectForKey:@"pulse"];
self.pulseTextField = cell.textField;
break;
case 3:
cell.vitalsLabel.text = @"Respiratory Rate";
cell.textField.text = [self.vitalsDictionary objectForKey:@"respiratory_rate"];
self.respiratoryRateTextField = cell.textField;
break;
case 4:
cell.vitalsLabel.text = @"Oxygen Saturation";
cell.textField.text = [self.vitalsDictionary objectForKey:@"oxygen_saturation"];
self.oxygenSaturationTextField = cell.textField;;
break;
case 5:
cell.vitalsLabel.text = @"Height";
cell.textField.text = [self.vitalsDictionary objectForKey:@"height"];
self.heightTextField = cell.textField;
break;
case 6:
cell.vitalsLabel.text = @"Weight";
cell.textField.text = [self.vitalsDictionary objectForKey:@"weight"];
self.weightTextField = cell.textField;
break;
case 7:
cell.vitalsLabel.text = @"BMI";
cell.textField.text = [self.vitalsDictionary objectForKey:@"bmi"];
self.bmiTextField = cell.textField;
break;
case 8:
cell.vitalsLabel.text = @"Pain (1-10)";
cell.textField.text = [self.vitalsDictionary objectForKey:@"pain"];
self.painTextField = cell.textField;
break;
default:
break;
}
return cell;
}
- (void) textFieldDidEndEditing:(UITextField *)textField{
if (textField == temperatureTextField){
[self.vitalsDictionary setObject:self.temperatureTextField.text forKey:@"temperature"];
}
else if (textField == pulseTextField){
[self.vitalsDictionary setObject:self.pulseTextField.text forKey:@"pulse"];
}
else if (textField == respiratoryRateTextField){
[self.vitalsDictionary setObject:self.respiratoryRateTextField.text forKey:@"respiratory_rate"];
}
else if (textField == oxygenSaturationTextField){
[self.vitalsDictionary setObject:self.oxygenSaturationTextField.text forKey:@"oxygen_saturation"];
}
else if (textField == heightTextField){
[self.vitalsDictionary setObject:self.heightTextField.text forKey:@"height"];
}
else if (textField == weightTextField){
[self.vitalsDictionary setObject:self.weightTextField.text forKey:@"weight"];
}
else if (textField == bmiTextField){
[self.vitalsDictionary setObject:self.bmiTextField.text forKey:@"bmi"];
}
else if (textField == painTextField){
[self.vitalsDictionary setObject:self.painTextField.text forKey:@"pain"];
}
else if (textField == bloodPressureTextField1){
[self.vitalsDictionary setObject:self.bloodPressureTextField1.text forKey:@"blood_pressure_1"];
}
else if (textField == bloodPressureTextField2){
[self.vitalsDictionary setObject:self.bloodPressureTextField2.text forKey:@"blood_pressure_2"];
}
}
In addition to @Barum's correct answer with regard to how you are accessing text fields you also have a problem creating cells. You are dequeuing cached cells but I don't see your code creating a new cell if dequeuing returns nil. You need to do the following:
Note: the example above assumes you define an enum for your tag values. I recommend this as it makes the code more readable and you won't have to remember which integer is which field type.
You cannot check by doing
textField == pulseTextField
, becauseUITableview
will reuse cells in a unpredictable manner as long as the reuse identifiers match. You are storing a reference to a text field, but when you scroll up and down, that cell may get assigned a different index. So, you may end up with duplicate reference to the same text field, since some cells may be off-screen with outdated duplicate reference.