UITextField随机重用tableview中的文本

时间:2011-11-23 23:12:55

标签: iphone objective-c cocoa-touch uitableview uitextfield

我的表中存在一些问题,每个单元格中都有UITextFields。我注意到文本仍然在错误的单元格中被随机重用,即使我似乎正确地保存它。

- (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 个答案:

答案 0 :(得分:3)

textFieldDidEndEditing:中看起来像是简单的拼写错误,您总是在temperatureTextField保存值。将您的代码更改为:

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