Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Cocoa 在插入时开始编辑_Cocoa_Core Data_Binding_Tableview_Edit - Fatal编程技术网

Cocoa 在插入时开始编辑

Cocoa 在插入时开始编辑,cocoa,core-data,binding,tableview,edit,Cocoa,Core Data,Binding,Tableview,Edit,我有一个使用核心数据的基于文档的应用程序,还有一个使用绑定和IB中的NSArrayController填充的tableView。一切正常,但我想确保第一列在向数组添加新对象时立即处于编辑模式。 我四处寻找合适的代码,并从Hillegass的书中改编了一些代码。编辑工作正常,但行选择似乎不起作用。它保持在第0行,编辑后进行制表时,要编辑的下一列位于该行。这是我使用过的代码的一个版本。我已经在谷歌上搜索了我的问题的解决方案,但结果要么没有核心数据,要么没有绑定,要么没有文档应用程序。 我是不是遗漏了

我有一个使用核心数据的基于文档的应用程序,还有一个使用绑定和IB中的NSArrayController填充的tableView。一切正常,但我想确保第一列在向数组添加新对象时立即处于编辑模式。 我四处寻找合适的代码,并从Hillegass的书中改编了一些代码。编辑工作正常,但行选择似乎不起作用。它保持在第0行,编辑后进行制表时,要编辑的下一列位于该行。这是我使用过的代码的一个版本。我已经在谷歌上搜索了我的问题的解决方案,但结果要么没有核心数据,要么没有绑定,要么没有文档应用程序。 我是不是遗漏了什么

-(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];
}

这是固定的(非常简单的)版本


}

首先,不需要强制转换对象的插入
-insertNewObjectForEntityForName:inManagedObjectContext:
返回
id
,因此强制转换是冗余的

您不需要仅仅为了获取数组而获取对象。您可以查询添加的新对象的
NSArrayController
,如果它不在那里,请添加它(很抱歉,我使用了
NSArrayController
,不记得它是否需要运行循环来检测更改)


从那里,您可以向NSArrayController询问索引并继续。

Aha,谢谢您,Zarra先生。。!我想我太聪明了,不利于我自己。现在工作得很好。再次感谢(顺便说一句,就像你的核心数据书一样)。整理好了。现在回想起来,我真想知道我到底在想什么……关于你的固定版本:你过早地发布了新员工,然后在调用
indexOfObjectIdenticalTo:
时使用它。使用自动释放,或者稍后释放。好的,我在底部释放了它。谢谢
-(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];