UITextField randomly reusing text in tableview

徘徊边缘 提交于 2019-12-13 03:18:25

问题


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:


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


来源:https://stackoverflow.com/questions/8250354/uitextfield-randomly-reusing-text-in-tableview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!