Pergunta

I have a NSOutlineView that show the content controlled by a NSTreeController which I bind to an NSMutableArray (arrayOfFiles). The array contains NSTreeNode objects where the representedObject (Fileobject class) holds a number of ivars. I would like to edit and update the ivar named "direction" for specific objects. I manage to get my object of interest using a NSIndexPath which I have stored for each object.

[self.myOutlineViewController.myTreeController setSelectionIndexPath:myIndexPath];

Fileobject *myObject =[[[self.myOutlineViewController.myTreeController.arrangedObjects descendantNodeAtIndexPath:myIndexPath] representedObject] representedObject];

[myObject setDirection:0];

This works fine, but I run into problems when I want to update the object I just extracted at NSIndexPath. The following crashes:[self.myOutlineViewController.myTreeController removeObjectAtArrangedObjectIndexPath:myIndexPath]; with the error message

An uncaught exception was raised
2012-11-08 17:23:25.557 S3access[20379:12b03] *** -[NSKeyValueSlowMutableArray     removeObjectsAtIndexes:]: value for key myArrayOfFiles of object 0x40012e460 is nil

I understand i am doing something wrong with Key-Value coding here but I am unable to see what it is. I have tried to seek solutions in all of the examples from Apple but I can only find examples that do not use NSTreeController and NSTreeNode. My thought was to 1) extract the object at indexpath 2) edit the extracted object 3) remove current object at indexpath 4) insert my new edited object into indexpath using command [self.myOutlineViewController.myTreeController insertObject:myNode atArrangedObjectIndexPath:myIndexPath];. I dont see how I can replace my object using Key-Value coding when I dont replace only an ivar but the whole object?

Any suggestions for what I am doing wrong and suggestions for how I may solve this is highly appreciated.

Cheers, and thanks! Trond

Foi útil?

Solução

I finally managed to get my update of NSOutlineView to work properly, and since my question was an extreme tumbleweed I thought I would at least update with an answer. It turns out my problem was more a lack of proper understanding of KVC rather than a programming problem. After having read answers to questions on Stackoverflow and the Apple documentation I finally figured out how to enable a dynamic update of my NSOutlineView and its NSTreeController contents (arrangedObjects) which are represented using NSTreeNode. The following code worked for me assuming you know the NSIndexPath (myIndexPath) of your object:

[self.myOutlineViewController.myOutlineView willChangeValueForKey:@"direction"];
[[[[self.myOutlineViewController.myTreeController.arrangedObjects descendantNodeAtIndexPath:myIndexPath] representedObject] representedObject] setDirection:0];
[self.myOutlineViewController.myOutlineView didChangeValueForKey:@"direction"];

Hopefully this may help some others. Cheers, Trond

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top