UITextField randomly reusing text in tableview

2019-08-30 05:27发布

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.

- (void) textFieldDidEndEditing:(UITextField *)textField{
    if (textField == temperatureTextField){
        [self.vitalsDictionary setObject:self.temperatureTextField.text forKey:@"temperature"];
    }
    else if (textField == pulseTextField){
        [self.vitalsDictionary setObject:self.temperatureTextField.text forKey:@"pulse"];
    }
    else if (textField == respiratoryRateTextField){
        [self.vitalsDictionary setObject:self.temperatureTextField.text forKey:@"respiratory_rate"];
    }
}

CellForRowAtIndex:

        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;

1条回答
倾城 Initia
2楼-- · 2019-08-30 06:19

Looks like simple typos in textFieldDidEndEditing: where you are always saving the value from temperatureTextField. Change your code to:

- (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"];
    }
}
查看更多
登录 后发表回答