1

i have this simplified Model: https://i.stack.imgur.com/d2WsV.png

I have these Elements ordered in an NSOutlineView and Controlled by an NSTreeController.

So I created some dummy data:

Folder1
|___ SubFolder1
     |___Element1
     |___Element2
|___ SubFolder2
     |___SubSubFolder1
         |___Element3

The Problem is now: How can I get an array that holds Element1, Element2, Element3 if I select Folder1 and that holds Element1 and Element2 if I select Subfolder1?

My first approach was to create a new NSArrayController bound to an custom property of my NSTreeController Object (Iderived a class for that) but that property only called once and not updated anymore after that. My second approach was to write a Fetch Predicate, but I wasnt successfull... :(

Any ideas?

monavari-lebrecht
  • 900
  • 3
  • 9
  • 23

1 Answers1

2

In order to make sure that changes to your structure are reflected in your custom property, you must define a dependency between keys and keypaths (see -keyPathsAffectingValueForKey:). However, because your keypaths are based on dynamic paths, you can't really do it that way, or you'd end up with an ever-growing list of dependencies.

So you have to do it a bit more manually. Every time you change a node in the tree, you will have to send -willChangeValueForKey: and -didChangeValueForKey: messages to your custom tree controller object, and that will trigger a reload of your flattened tree array. Just make sure you actually re-create the array, too. I assume you're walking the tree and flattening it manually.

Bored Astronaut
  • 994
  • 5
  • 10
  • sorry I cant vote you up (I dont have enough reputation), but that solves my problem THX!!! :) **- (void)didChangeValueForKey:(NSString *)key { if ([key isEqualToString:@"selection"]) { [self didChangeValueForKey:@"allSubelements"]; } [super didChangeValueForKey:key]; }** – monavari-lebrecht Jul 06 '11 at 19:28