背景
Cocoa アプリでソース リスト (iTunes などと同様) を作成しました。
Value列がNSTreeControllerのarrangedObjects.nameキーパスにバインドされたNSOutlineViewがあります。
NSTreeController は、コア データ ストア内の JGSourceListNode エンティティにアクセスします。
JGSourceListNode の 3 つのサブクラス - JGProjectNode、JGGroupNode、および JGFolderNode があります。
App Delegate で selectedIndexPaths と呼ばれる NSArray にバインドされた NSTreeController の selectedIndexPaths があります。
起動時にグループ ノードを検索し、コア データ ストアに見つからない場合は作成します。
if ([allGroupNodes count] == 0) {
JGGroupNode *rootTrainingNode = [JGGroupNode insertInManagedObjectContext:context];
[rootTrainingNode setNodeName:@"TRAIN"];
JGProjectNode *childUntrainedNode = [JGProjectNode insertInManagedObjectContext:context];
[childUntrainedNode setParent:rootTrainingNode];
[childUntrainedNode setNodeName:@"Untrained"];
JGGroupNode *rootBrowsingNode = [JGGroupNode insertInManagedObjectContext:context];
[rootBrowsingNode setNodeName:@"BROWSE"];
JGFolderNode *childFolder = [JGFolderNode insertInManagedObjectContext:context];
[childFolder setNodeName:@"Folder"];
[childFolder setParent:rootBrowsingNode];
[context save:nil];
}
私が欲しいもの
アプリを起動すると、トップ レベルのグループが両方とも展開され、「訓練されていない」が次のように強調表示されます。
マイ ウィンドウ http://synapticmishap.co.uk/Window.jpeg
問題
applicationDidFinishLaunching:
アプリ デリゲートのメソッドに次のコードを追加します。
[sourceListOutlineView expandItem:[sourceListOutlineView itemAtRow:0]];
[sourceListOutlineView expandItem:[sourceListOutlineView itemAtRow:2]];
NSIndexPath *rootIndexPath = [NSIndexPath indexPathWithIndex:0];
NSIndexPath *childIndexPath = [rootIndexPath indexPathByAddingIndex:0];
[self setSelectedIndexPaths:[NSArray arrayWithObject:childIndexPath]];
しかし、アウトライン ビューはまだ準備されていないようで、このコードは何もしません。
理想的には、最終的にユーザーが行った最後の選択を保存し、再起動時にこれを復元したいと考えています。
質問
クレイジーな KVO を使用して NSTreeController または NSOutlineView がいつ読み込まれるかを観察し、項目を展開して選択を変更することは可能だと確信していますが、それはぎこちなく、回避策のように感じます。
これをエレガントにするにはどうすればよいですか?