2011-02-01 8 views
2

私はコアデータを使用するアプリケーションベースのアプリケーションを持っており、バインディングと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]; 

}

ここで固定(と大幅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

+0

あなたの修正版について:あなたはnewEmployeeをあまりにも早くリリースし、それを 'indexOfObjectIdenticalTo:'の呼び出しで使用しています。 autoreleaseを使用するか、後で解放してください。 –

+0

いいえ、私は代わりに下にそれをリリースしました。ありがとう! – BillySangster

答えて

1

まずだ、あなたは、オブジェクトの挿入をキャストする必要はありません。 -insertNewObjectForEntityForName: inManagedObjectContext:idを返しますので、キャストは還元剤です。

アレイを取得するためだけにオブジェクトを取得する必要はありません。あなたが追加した新しいオブジェクトについてNSArrayControllerを照会することができます。もしそれがなければ、追加してください(私はNSArrayControllerを使用していたので、しばらくお待ちください、変更を検出するために実行ループのサイクルが必要かどうかはわかりません)。

そこから、NSArrayControllerにインデックスを尋ねて、次に進むことができます。

+0

ああ、Zarraさん、ありがとう!私は自分の利益のためにはあまりにも賢明にしようとしていたと思う。完璧に今働いている。もう一度おねがいします(ちなみに、Core Dataブックのように)。 – BillySangster

関連する問題