我已经有一个基于文档的应用程序使用核心数据,并有一个tableView使用IB中的绑定和NSArrayController填充。一切工作正常,但我想确保第一列将立即在编辑模式下添加一个新的对象到数组。 我四处寻找合适的代码并修改了一些Hillegass书。编辑工作正常,但行选择似乎没有工作。它停留在第0行,并且在编辑要编辑的下一列之后的Tab键位于该行上。这是我用过的代码的一个版本。我已经搜索了我的问题的解决方案,但不断收到没有核心数据或绑定或非文档应用程序的结果。 我在这里错过了什么吗?开始编辑插入
-(IBAction)addEmployee:(id)sender {
Employee *newEmployee = (Employee *)[NSEntityDescription insertNewObjectForEntityForName:@"Employee" inManagedObjectContext:[self managedObjectContext]];
//Fetch the update Employees
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *employeeEntity = [NSEntityDescription entityForName:@"Employee" inManagedObjectContext:[self managedObjectContext]];
[fetchRequest setEntity:employeeEntity];
NSArray *fetchResults = [[self managedObjectContext] executeFetchRequest:fetchRequest error:&error];
[fetchRequest release];
NSError *error = nil;
if (fetchResults == nil) {
// Something here to handle the error.
NSLog(@"The fetch results is null");
}
int row = [fetchResults indexOfObjectIdenticalTo:newEmployee];
NSLog(@"row is %d",row);
[[tableView window] endEditingFor:nil];
[tableView editColumn:1 row:row withEvent:nil select:YES];
}
这里是固定的(并且大大simpified)版本
-(IBAction)addEmployee:(id)sender {
Employee *newEmployee = [newEmployeeController newObject];
[newEmployeeController addObject:newEmployee];
[newEmployeeController rearrangeObjects];
NSArray *fetchResults = [newEmployeeController arrangedObjects];
int row = [fetchResults indexOfObjectIdenticalTo:newEmployee];
[[tableView window] endEditingFor:nil];
[tableView editColumn:1 row:row withEvent:nil select:YES];
[newEmployee release];
}
排序。现在我回过头来,严肃地想知道我在想什么...... – BillySangster 2011-02-01 22:24:57
关于你的固定版本:你过早释放newEmployee,然后在调用`indexOfObjectIdenticalTo:`时使用它。使用autorelease,或者稍后再发布。 – 2011-09-08 18:16:35