【问题标题】:Save Text from UITableViews从 UITableViews 保存文本
【发布时间】:2010-12-02 17:25:18
【问题描述】:

编辑:找到崩溃的实际原因。当我更改数据模型时,我需要删除并重新安装才能正确测试而不会崩溃。然而,它现在是一个解决如何使用 coredata 正确保存数组的案例。我该怎么做?

我有一个 UITableView,我希望能够在单元格中输入文本,然后将该文本保存在数组中,然后在加载应用程序时从数组中重新加载文本。

我正在使用 CoreData,但似乎遇到了一些问题。这是我到目前为止所拥有的。

在 cellForRowAtIndex 函数中,我将 UITextFields 添加为每个单元格的子视图,因此我可以输入它们,并且我只能在我的 UISwitch、editCells 开启时输入它们。其中四个单元格中有我设置的预设文本。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(15, 20, 185, 30)];
    textField.delegate = self;

    // Configure the cell...

    if ([indexPath row] == 0) {
        [cell setAccessoryView:textField];
        textField.text = @"Before School";
        textField.font = [UIFont fontWithName:@"Helvetica-Oblique" size:16];
        textField.enabled = FALSE;
    }

    else if ([indexPath row] == 3) {
        [cell setAccessoryView:textField];
        textField.text = @"Break Time";
        textField.font = [UIFont fontWithName:@"Helvetica-Oblique" size:16];
        textField.enabled = FALSE;
    }

    else if ([indexPath row] == 6) {
        [cell setAccessoryView:textField];
        textField.text = @"Lunchtime";
        textField.font = [UIFont fontWithName:@"Helvetica-Oblique" size:16];
        textField.enabled = FALSE;
    }

    else if ([indexPath row] == 9) {
        [cell setAccessoryView:textField];
        textField.text = @"After School";
        textField.font = [UIFont fontWithName:@"Helvetica-Oblique" size:16];
        textField.enabled = FALSE;
    }

    else if ([indexPath row] == 1 || [indexPath row] == 2 || [indexPath row] == 4 || [indexPath row] == 5 || [indexPath row] == 7 || [indexPath row] == 8) {
        [cell setAccessoryView:textField];
        textField.font = [UIFont fontWithName:@"Helvetica" size:16];
        if (editCells.on == TRUE) {
            textField.enabled = TRUE;
        }
        else {
            textField.enabled = FALSE;
        }
    }

    [monArrayA addObject:textField.text];

    [textField release];

    return cell;
}

为了保存,我会在 editCells 的值更改时运行它,所以当用户完成编辑时,它的保存为这个开关将被关闭。我制作了一个新对象来保存核心数据。我保存的课程是包含所有单元格文本的数组和它需要在其中的表格,但保存它的标签。但当然,这会导致 SIGABRT 错误,并在轻按开关时使应用程序崩溃。

[editCells addTarget:self action:@selector(editCells:) forControlEvents:UIControlEventValueChanged];

-(void)editCells:(id)sender{
    [monTable reloadData];

    //Create a new object to save.
    NSManagedObject *newLesson;
    //Tell it to be saved to the DatedText entity using the managedObjectContext, context_.
    newLesson = [NSEntityDescription insertNewObjectForEntityForName:@"TimeTable" inManagedObjectContext:self.managedObjectContext];
    //Set two values within this object, being the text the user entered and the date from the datePicker.
    //Save these to their appropriate properties within this entity.
    [newLesson setValue:monArrayA forKey:@"lessonName"];
    [newLesson setValue:monTable.tag forKey:@"table"];

    //Create error incase of failure to save.
    NSError *saveError = nil;
    //Try and access the data store via context_ and fall back on saveError if fails.
    [self.managedObjectContext save:&saveError];
    if (saveError != nil) {
        //Report error by printing it to the console.
        NSLog(@"[%@ saveContext] Error saving context: Error = %@, details = %@",[self class], saveError,saveError.userInfo);
    }
}

【问题讨论】:

  • 具体是哪里崩溃了?
  • 找到了崩溃的实际原因。当我更改数据模型时,我需要删除并重新安装才能正确测试而不会崩溃。然而,它现在是一个解决如何使用 coredata 正确保存数组的案例。

标签: iphone ipad core-data uitableview


【解决方案1】:

Tableview 的工作方式与您的预期略有不同。要记住的重要一点是,调用 tableView:cellForRowAtIndexPath: 既可以创建一个单元格,也可以重用一个单元格。您应该只在 if (cell == nil) 路径中创建子视图(如您的 textField)。确保给每个字段一个唯一的标签,并将它们添加为单元格的子视图。

然后,在 configure cell 路径中(在可能创建元素之后),您可以通过调用 [cell.contentView viewWithTag:YOUR_FIELD_TAG] 再次获取字段,然后更新字段。

【讨论】:

  • 对此 Denis 的附带问题 - 如果您的 textField 被重复使用,您是否可能在不同的单元格中使用相同的 textField(及其关联的标签)?
  • 这就是我想知道的罗格。另外标记每个字段有什么用?他们仍然获得单独的文本值,那么标记需要什么?这似乎对我的储蓄问题没有帮助,除非我误解了你的意图。
  • 我刚刚意识到,保存可能不起作用,因为我试图将数组保存到我的数据模型中的属性中,该属性是一个字符串。但是我该如何保存呢?没有保存 NSMutableArray 的选项。
猜你喜欢
  • 2011-04-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-31
  • 2014-10-22
  • 2014-08-03
相关资源
最近更新 更多