如何从这些数据中创建NSDictionary?

时间:2011-11-23 22:14:57

标签: iphone objective-c uitableview uitextfield nsdictionary

我有一个包含单元格的tableview,每个单元格都有一个UITextField。现在我手动将这些数据从NSDictionary设置到每个单元格,并且我将UITextField的属性设置为每个单元格的UITextField。这将textField的文本设置为字典中的文本(如果有),但如果没有,我希望用户能够将文本设置为新的NSDictionary,我可以将其上传回服务器

但我想我需要创建一个NSDictionary来存储新值。现在,当表格在视图中滚动时,单元格中的文本将被删除并随机重复使用。

else if (tableView == self.vitalsTableView)
    {
        if ([indexPath row] == 2) {
            CellIdentifier = @"bloodPressureCell";
            BloodPressureTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
            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.childAppointmentDictionary objectForKey:@"temperature"];
                    self.temperatureTextField = cell.textField;
                    break;
                case 1:
                    cell.vitalsLabel.text = @"Pulse";
                    cell.textField.text = [self.childAppointmentDictionary objectForKey:@"pulse"];
                    self.pulseTextField = cell.textField;
                    break;
                case 3:
                    cell.textField.text = [self.childAppointmentDictionary objectForKey:@"respiratory_rate"];
                    self.respiratoryRateTextField = cell.textField;
                    break;
                case 4:
                    cell.vitalsLabel.text = @"Oxygen Saturation";
                    cell.textField.text = [self.childAppointmentDictionary objectForKey:@"oxygen_saturation"];
                    self.oxygenSaturationTextField = cell.textField;;
                    break;
                case 5:
                    cell.vitalsLabel.text = @"Height";
                    cell.textField.text = [self.childAppointmentDictionary objectForKey:@"height"];
                    self.heightTextField = cell.textField;
                    break;
                case 6:
                    cell.vitalsLabel.text = @"Weight";
                    cell.textField.text = [self.childAppointmentDictionary objectForKey:@"weight"];
                    self.weightTextField = cell.textField;
                    break;
                case 7:
                    cell.vitalsLabel.text = @"BMI";
                    cell.textField.text = [self.childAppointmentDictionary objectForKey:@"bmi"];
                    self.bmiTextField = cell.textField;
                    break;
                case 8:
                    cell.vitalsLabel.text = @"Pain (1-10)";
                    cell.textField.text = [self.childAppointmentDictionary objectForKey:@"pain"];
                    self.painTextField = cell.textField;
                    break;
                default:
                    break;
            }
            return cell;
        }
    }

1 个答案:

答案 0 :(得分:0)

离开屏幕时会破坏单元格,因此其中不存在的任何数据也会被销毁。

单元格是视图 - 而不是模型 - 因此请确保将数据保存在控制器要求提供给视图的模型中。