0

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"];
    }
}

2 Answers 2

0

You cannot check by doing textField == pulseTextField, because UITableview 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.

3
  • One option is to utilize tag, a UIView property. That way UITextField will have a single tag attached to it, so you can identify what it is used for. Note that tag is just NSInteger, so you should probably declare some constants first.
    – Barum Rho
    Nov 24, 2011 at 1:22
  • That sounds perfect, can you help me with this as I haven't used constants before? I have 11 cells as shown in the example code. Im thinking instead of setting self.temperatureTextField = cell.textField;, would do self.temperatureTextField.tag = 1 or something similar
    – Jon
    Nov 24, 2011 at 1:24
  • @Barum is correct, you should use tags to access your textFields. Use cell.textField.tag = 1; when creating a cell. make sure you use a unique integer for each type of textField possible within the cell. In textFieldDidEndEditing: use if (textField.tag == TAG_VALUE) to check for a specific type of textField. I see another issue which I'll put in a separate answer.
    – XJones
    Nov 24, 2011 at 1:47
0

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:

else if (tableView == self.vitalsTableView) {
    if ([indexPath row] == 2) {
        CellIdentifier = @"bloodPressureCell";
        BloodPressureTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            // create cell if nothing dequeued
            cell = [[BloodPressureTableViewCell alloc] init...]; // use an appropriate initializer
            cell.textField1.tag = MyTextFieldTagBloodPressure1;
            cell.textField2.tag = MyTextFieldTagBloodPressure2; 
        }
        // configure the cell
        cell.textField1.text = [self.vitalsDictionary objectForKey:@"blood_pressure_1"];
        cell.textField2.text = [self.vitalsDictionary objectForKey:@"blood_pressure_2"];
        return cell;
    }
    // do the same for all cell types

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.