2011-02-01 62 views
2

我已经有一个基于文档的应用程序使用核心数据,并有一个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]; 

}

+0

排序。现在我回过头来,严肃地想知道我在想什么...... – BillySangster 2011-02-01 22:24:57

+0

关于你的固定版本:你过早释放newEmployee,然后在调用`indexOfObjectIdenticalTo:`时使用它。使用autorelease,或者稍后再发布。 – 2011-09-08 18:16:35

回答

1

首先,你不需要把对象的插入。 -insertNewObjectForEntityForName: inManagedObjectContext:返回id所以演员阵容减少。

您不需要为获取数组而获取对象。您可以在NSArrayController中查询您添加的新对象(如果不存在),请添加它(对不起,因为我使用了NSArrayController,并且不记得它是否需要运行循环的一个循环来检测更改)。

从那里你可以要求NSArrayController索引,并继续前进。