Objective c 表视图仅在应用程序重新启动后显示
现在我的问题不是一个而是两个,如下所示:Objective c 表视图仅在应用程序重新启动后显示,objective-c,core-data,Objective C,Core Data,现在我的问题不是一个而是两个,如下所示: (void)saveDetails{ EmployeeDetailsAppDelegate *appDelegate = (EmployeeDetailsAppDelegate *) [[UIApplication sharedApplication] delegate]; NSManagedObjectContext *context = [appDelegate managedObjectContext]; NSManagedObj
(void)saveDetails{
EmployeeDetailsAppDelegate *appDelegate = (EmployeeDetailsAppDelegate *) [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSManagedObject *newDetails;
newDetails = [NSEntityDescription insertNewObjectForEntityForName:@"Details" inManagedObjectContext:context];
[newDetails setValue:empID.text forKey:@"EmployeeID"];
[newDetails setValue:empName.text forKey:@"EmployeeName"];
[newDetails setValue:empDepartment.text forKey:@"EmployeeDepartment"];
empID.text = @"";
empName.text = @"";
empDepartment.text = @"";
[[appDelegate rootViewController]addNewObject:empName.text];
NSError *error;
[context save:&error];
[self dismissModalViewControllerAnimated:YES];
}
应用程序要求:输入员工详细信息并单击保存按钮。员工的姓名应添加到表视图中。单击一行,详细信息应显示在详细信息视图中
问题1:第一次单击“保存”按钮后,它将显示在表视图中,但从下一次单击开始,直到重新启动应用程序,它才会显示。我的代码如下:
(void)saveDetails{
EmployeeDetailsAppDelegate *appDelegate = (EmployeeDetailsAppDelegate *) [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSManagedObject *newDetails;
newDetails = [NSEntityDescription insertNewObjectForEntityForName:@"Details" inManagedObjectContext:context];
[newDetails setValue:empID.text forKey:@"EmployeeID"];
[newDetails setValue:empName.text forKey:@"EmployeeName"];
[newDetails setValue:empDepartment.text forKey:@"EmployeeDepartment"];
empID.text = @"";
empName.text = @"";
empDepartment.text = @"";
[[appDelegate rootViewController]addNewObject:empName.text];
NSError *error;
[context save:&error];
[self dismissModalViewControllerAnimated:YES];
}
问题2:这基本上不是一个问题,而是一个查询。单击表视图中的行时,员工部门将显示在详细信息视图中。现在我还想显示员工id。我该怎么做?我的员工部门显示代码为:
(void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if([self.array count]){
dictionary = [self.array objectAtIndex:indexPath.row] ;
if([dictionary objectForKey:@"EmployeeDepartment"]){
[dictionary objectForKey:@"EmployeeDepartment"];
NSString *empDep = [dictionary objectForKey:@"EmployeeDepartment"];
NSLog(@"Dep is %@",empDep);
}
}
detailViewController.detailItem = [NSString stringWithFormat:@"Employee Department: %@",[dictionary objectForKey:@"EmployeeDepartment"]];
self.navigationItem.leftBarButtonItem.enabled = YES;
}
PS:这是用于核心数据用作存储的iPad 当您在UITableView中使用核心数据时,一定要使用a及其方法 如果我正确理解了你的第一个问题,这应该可以解决它。它还将解决更多的问题,这些问题将在以后的数据阵列中出现,这些数据阵列将尝试镜像核心数据对象。我说试试,因为它显然不起作用 简而言之,NSFetchedResultsController“监视”要在tableview中显示的核心数据对象,如果对象发生更改,它将通知您的代理,由其执行相应的操作(如插入或删除行)。文档中有您必须实现的委托方法的完整示例代码 关于第二个问题:更改DetailViewController,使其能够处理NSManagedObject。无法编辑对象的detailView通常是无用的,因此您迟早需要这样做