Iphone 从UITableView保存文本
编辑:找到崩溃的实际原因。我需要删除并重新安装,以便在更改数据模型时正确测试而不会崩溃。然而,现在需要解决如何正确地使用coredata保存数组的问题。我该怎么做 我有一个UITableView,我希望能够在单元格中输入文本,然后将该文本保存在数组中,然后在加载应用程序时从数组中重新加载文本 我正在使用CoreData,但似乎有一些问题。以下是我到目前为止的情况 在CellForRowatinex函数中,我将UITextFields添加为每个单元格的子视图,这样我就可以输入它们,并且我只能在UISwitch editCells打开时输入它们。其中四个单元格中有我设置的预设文本Iphone 从UITableView保存文本,iphone,ipad,core-data,uitableview,Iphone,Ipad,Core Data,Uitableview,编辑:找到崩溃的实际原因。我需要删除并重新安装,以便在更改数据模型时正确测试而不会崩溃。然而,现在需要解决如何正确地使用coredata保存数组的问题。我该怎么做 我有一个UITableView,我希望能够在单元格中输入文本,然后将该文本保存在数组中,然后在加载应用程序时从数组中重新加载文本 我正在使用CoreData,但似乎有一些问题。以下是我到目前为止的情况 在CellForRowatinex函数中,我将UITextFields添加为每个单元格的子视图,这样我就可以输入它们,并且我只能在UI
- (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);
}
}
TableView的工作方式与您预期的略有不同。需要记住的重要一点是,调用tableView:cellforrowatinexpath:既可以创建一个单元格,也可以重用一个单元格。您应该只在
if(cell==nil)
路径中创建子视图(如文本字段)。确保为每个字段指定一个唯一的标记,并将其添加为单元格的子视图
然后,在
configure cell
路径中(在可能创建元素之后),通过调用[cell.contentView viewWithTag:YOUR_FIELD_TAG]
再次获取字段,然后更新字段。具体崩溃在哪里?找到了崩溃的实际原因。我需要删除并重新安装,以便在更改数据模型时正确测试而不会崩溃。然而,现在的问题是如何正确地使用coredata保存数组。Denis的一个附带问题是:如果您的textField被重用,您是否可能在不同的单元格中使用相同的textField(及其相关标记)?这就是我想知道的。另外,标记每个字段有什么用?它们仍然会得到单独的文本值,那么在标记中需要什么呢?这似乎对我的保存问题没有帮助,除非我误解了您的意图。我刚刚意识到,保存可能不起作用,因为我试图将数组保存到数据模型中的属性中,该属性是字符串。但我怎样才能拯救它呢?没有保存NSMutableArray的选项。