UITextField随机重用UITextField中的文本

时间:2011-11-24 00:16:26

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

这是此问题的扩展:UITextField randomly reusing text in tableview

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

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

答案 0 :(得分:0)

您无法通过textField == pulseTextField进行检查,因为UITableview只要重用标识符匹配,就会以不可预测的方式重复使用单元格。您正在存储对文本字段的引用,但是当您向上和向下滚动时,可能会为该单元格分配不同的索引。因此,您最终可能会对同一文本字段进行重复引用,因为某些单元格可能会在屏幕外显示过时的重复引用。

答案 1 :(得分:0)

除了@ Barum关于如何访问文本字段的正确答案之外,您还在创建单元格时遇到问题。您正在使缓存的单元格出列但我没有看到您的代码创建一个新的单元格,如果出列返回nil。您需要执行以下操作:

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

注意:上面的示例假定您为标记值定义枚举。我推荐这个,因为它使代码更具可读性,你不必记住哪个整数是哪种字段类型。