编辑:找到崩溃的实际原因。当我更改数据模型时,我需要删除并重新安装以正确测试而不会崩溃。然而,现在它的一个案例是如何正确保存带有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);
}
}
答案 0 :(得分:0)
Tableviews的工作方式与您期望的略有不同。要记住的重要一点是,tableView:cellForRowAtIndexPath:既可以创建一个单元格,也可以重用一个单元格。您应该只在if (cell == nil)
路径中创建子视图(例如textField)。确保为每个字段指定一个唯一标记,并将它们添加为单元格的子视图。
然后,在configure cell
路径中(在可能创建元素之后),您可以通过调用[cell.contentView viewWithTag:YOUR_FIELD_TAG]
再次获取字段,然后更新字段。