How can I iterate through all items of my NSOutlineView? - objective-c

I need to perform an operation on all objects in my NSOutlineView when I close the window.
(Both parents and children, and children of children).
It doesn't matter if the items are expanded or not, I just need to perform a selector over all items in the outline view.
thanks

Assuming you're using NSOutlineViewDataSource and not bindings, you could do it like this:
- (void)iterateItemsInOutlineView: (NSOutlineView*)outlineView
{
id<NSOutlineViewDataSource> dataSource = outlineView.dataSource;
NSMutableArray* stack = [NSMutableArray array];
do
{
// Pop an item off the stack
id currentItem = stack.lastObject;
if (stack.count)
[stack removeLastObject];
// Push the children onto the stack
const NSUInteger childCount = [dataSource outlineView: outlineView numberOfChildrenOfItem: currentItem];
for (NSUInteger i = 0; i < childCount; ++i)
[stack addObject: [dataSource outlineView: outlineView child: i ofItem: currentItem]];
// Visit the current item.
if (nil != currentItem)
{
// Do whatever you want to do to each item here...
}
} while (stack.count);
}
This should achieve a full traversal of all the objects vended by your NSOutlineViewDataSource.
FYI: If you're using cocoa bindings, this won't work as is. If that's the case, though, you could use a similar approach (i.e. surrogate-stack traversal) against the NSTreeController (or whatever else) you're binding to.
HTH

Related

NSOutlineView: Index of Selected Parent Item

I'm using NSOutlineView (cell-based) for the first time. I need to return the index of the selected parent item.
In the picture above, there are two parent items. I want to return 1 if I select Colors 2. The application will return 1 if Colors is not expanded. If it's expanded, it will return 6.
- (void)outlineViewSelectionDidChange:(NSNotification *)notification {
NSOutlineView *outlineView = [notification object];
id item = [outlineView itemAtRow:[outlineView selectedRow]];
if ([item objectForKey:#"parent"]) {
NSInteger r = [outlineView selectedRow]; // returning 6
}
}
How can I return the index of the selected parent item correctly whether or not any of the parent items is expanded? It seems that my problem is related to this topic. Yet, since I'm not very familiar with NSOutlineView, I don't know what I can do to improve my code.
Muchos thankos.
The item returned by -[NSOutlineView itemAtRow:] is an NSTreeNode instance. These objects are created by Cocoa - I've never needed to create one myself - and are used to wrap the objects you passed to the outline view from your data source. They have two very useful properties: representedObject (of type id) and indexPath (of type NSIndexPath). The representedObject is the object that's being wrapped (one of your own model objects), whereas you can think of the indexPath of an item as a way of establishing its exact location in a fully expanded outline view - this is the what you're after. Once you've got your item, cast it to an NSTreeNode, then call its indexPath property. In the case you outline in your question the relevant index path will consist of just a single number (1), but index paths vary in length depending on where the associated item sits in the data 'tree`. For example the blue color that is revealed when you expand Colors 2 would have an index path of [0, 3]
- (void)outlineViewSelectionDidChange:(NSNotification *)notification {
NSOutlineView *outlineView = [notification object];
id item = [outlineView itemAtRow:[outlineView selectedRow]];
NSTreeNode *node = (NSTreeNode *)item;
NSIndexPath *ip = [node indexPath];
}

NSOutlineView shows only first 2 levels of the NSTreeController hierarchy

I'm populating a NSOUtlineView with a NSTreeController.
The NSTreeController is a 3 levels hierarchy controller (CBMovie, CBDisc and CBEpisode), but only the first 2 levels are displayed in the outline view.
The implementation is the same for all objects: I've implemented methods to specify children, children count and if the object is a leaf. These methods are correctly called for all objects (also for those ones that are not displayed, the grandchildren: CBEpisode).
In the outline View, everything is displayed correctly for the first 2 level. But grandchildren are never displayed, I don't have the option to expand their parent to see them. I can only see CBMovie and CBDiscs.
I'm wondering if there is another setting I'm missing, about how deep the nodes can expand in NSTreeControllers or NSOutlineView configurations.
Below: Implementation in one of the three nodes.
Each node class has different path to its children. This is specified in the -(NSArray*)children method (correctly called).
-(NSArray*)children
{
return [[self Episodes] allObjects];
}
-(int)childrenCount
{
return [[self Episodes] count];
}
-(BOOL)isLeaf
{
return ![[self Episodes] count];
}
Output of logging code. The datasource, the NSTreeController, seems to have the correct structure.
CBMovie
CBDisc
CBEpisode
CBEpisode
CBMovie
CBDisc
CBDisc
CBDisc
CBDisc
CBMovie
CBDisc
CBEpisode
CBEpisode
This is how I populate the NSOutlineView (cell based). I don't use datasource methods, but I'm binding it programmatically.
NSMutableDictionary *bindingOptions = [[NSMutableDictionary alloc] initWithCapacity:2];
if (metadata.valueTransformer) {
[bindingOptions setObject:metadata.valueTransformer forKey:NSValueTransformerNameBindingOption];
}
[bindingOptions setObject:[NSNumber numberWithBool:NO] forKey:NSCreatesSortDescriptorBindingOption];
[bindingOptions setObject:[NSNumber numberWithBool:NO] forKey:NSRaisesForNotApplicableKeysBindingOption];
[newColumn bind:#"value" toObject:currentItemsArrayController withKeyPath:[NSString stringWithFormat:#"arrangedObjects.%#", metadata.columnBindingKeyPath] options:bindingOptions];
Examining the NSTreeController side
The NSTreeController is a 3 levels hierarchy controller, but only the first 2 levels are displayed in the outline view.
Have you confirmed that all three levels are loaded into your NSTreeController? You could do this by logging its contents to the console with the extension below. If the output produced by this code matches what appears in your outline-view, the problem is probably on the NSTreeController side, not the outline-view.
#import "NSTreeController+Logging.h"
#implementation NSTreeController (Logging)
// Make sure this is declared in the associated header file.
-(void)logWithBlock:(NSString * (^)(NSTreeNode *))block {
NSArray *topNodes = [self.arrangedObjects childNodes];
[self logNodes:topNodes withIndent:#"" usingBlock:block];
}
// For internal use only.
-(void)logNodes:(NSArray *)nodes
withIndent:(NSString *)indent
usingBlock:(NSString * (^)(NSTreeNode *))block {
for (NSTreeNode * each in nodes) {
NSLog(#"%#%#", indent, block(each));
NSString *newIndent = [NSString stringWithFormat:#" %#", indent];
[self logNodes:each.childNodes withIndent:newIndent usingBlock:block];
}
}
#end
The above code does not need to be adjusted, all you need to do is call it with a customised block:
-(void)logIt {
[self.treeController logWithBlock:^NSString *(NSTreeNode * node) {
// This will be called for every node in the tree. This implementation
// will see the object's description logged to the console - you may
// want to do something more elaborate.
NSString *description = [[node representedObject] description];
return description;
}];
}
Examining the NSOutlineView side
If all the data seems to have loaded correctly into the NSTreeController you could have a look at how your NSOutlineView is populated. The delegate method -[NSOutlineViewDelegate outlineView:viewForTableColumn:item] is a good place to start. The item argument is the NSTreeNode instance, so, before you return the relevant view you can look at this node and make sure it's behaving as expected. In your case, you should scrutinize the properties of item objects that are representing CBDisc objects. (You may need to click on disclosure buttons to get this method to fire for the relevant objects.)
-(NSView *)outlineView:(NSOutlineView *)outlineView
viewForTableColumn:(NSTableColumn *)tableColumn
item:(id)item {
NSTreeNode *node = (NSTreeNode *)item;
NSManagedObject *representedObject = (NSManagedObject *)node.representedObject;
// Query the node
NSLog(#"%# <%#>", representedObject.description, [representedObject class]);
NSLog(#" node is a leafNode: %#", node.isLeaf ? #"YES" : #"NO");
NSLog(#" node has child-count of: %lu", (unsigned long)node.childNodes.count);
// Query the NSManagedObject
// your stuff here...
// This is app-specific - you'll probably need to change the identifier.
return [outlineView makeViewWithIdentifier:#"StandardTableCellView" owner:self];
}
So, I've figured out why.
Quite stupid: the main outline column containing the disclosure arrows was 20px width only and the arrows of the children had indentation.
I'm using the outline column for the arrows only and not the titles of the nodes, that's why it's so narrow.
I had disabled indentation and now I can see all arrows.

NSMutableArray Resetting Itself?

I am having an issue with NSMutableArray wiping its contents.
Consider my code: (int i; is in my file's .h as is NSMutableArray* newFileControllerArray)
-(void)awakeFromNib{
i = 0;
newFileWindowControllerArray = [[NSMutableArray alloc]init];
}
-(IBAction)newFileMenubar:(id)sender{
[newFileWindowControllerArray addObject:[[NewFileWindowController alloc]initWithWindowNibName:#"NewFileWindowController"]];
NSUInteger elementsInArray = [newFileWindowControllerArray count];
NSLog(#"%lu",(unsigned long)elementsInArray);
[[newFileWindowControllerArray objectAtIndex:i] showWindow:nil];
}
-(IBAction)OKButtonClicked:(id)sender{
NSUInteger elementsInArray = [newFileWindowControllerArray count];
NSLog(#"THERE ARE %lu ELEMENTS IN THE ARRAY",(unsigned long)elementsInArray);
}
The first method called (other than awakeFromNib:) is newFileMenubar: This will add one element to the array. I can confirm that this works because 1 is printed in the console. However, once OKbutton is called and I print out the number of elements in my array it says that no elements are in the array. Why is that?
Am I missing something very obvious here? Why does my array reset itself?
EDIT:
The comments have gotten long and unwieldy so here is the code w/NSLogs and outputs:
-(void)awakeFromNib{
i = 0;
newFileWindowControllerArray = [NSMutableArray array];
NSLog(#"self=%p, array=%p", self, newFileWindowControllerArray);
}
-(IBAction)newFileMenubar:(id)sender{
[newFileWindowControllerArray addObject:[[NewFileWindowController alloc]initWithWindowNibName:#"NewFileWindowController"]];
[[newFileWindowControllerArray objectAtIndex:i] showWindow:nil];
i++;
NSLog(#"self=%p, array=%p", self, newFileWindowControllerArray);
}
-(IBAction)OKButtonClicked:(id)sender{
NSUInteger elementsInArray = [newFileWindowControllerArray count];
NSLog(#"self=%p, array=%p", self, newFileWindowControllerArray);
[documentController newDocument:sender];
[[newFileWindowControllerArray objectAtIndex:i]close];
}
When the program launches, this is the output: self=0x100141480, array=0x100140f30
This should be coming from awakeFromNib:
The next method called is newFileMenubar:
The output from this is
self=0x1001ac990, array=0x1005228a0 and immediately after self=0x100141480, array=0x100140f30
The last method called is OKButtonClicked:
The output from the last method (OKButtonClicked:) is self=0x1001ac990, array=0x1005228a0
As you can see from the code, the name of the array doesn't change, but my outputs beg to differ? What could cause this?
There are good clues in your log output. There are multiple instances of the view controller (see the different values for 'self'?). They each have their own array. See this code...
-(IBAction)newFileMenubar:(id)sender{
[newFileWindowControllerArray addObject:[[NewFileWindowController alloc]initWithWindowNibName:#"NewFileWindowController"]];
When you press the button associated with that action, your app builds another view controller and places it in the array. That view controller gets the awake from nib message and allocates another array, and so on.
To confirm this, change the code as follows:
-(IBAction)newFileMenubar:(id)sender{
[newFileWindowControllerArray addObject:#"Hello world"];
// and comment this out, for now:
// [[newFileWindowControllerArray objectAtIndex:i] showWindow:nil];
In the other methods, comment out your expectations that the array has anything other than strings in it, and see what you get. e.g. ...
- (IBAction)OKButtonClicked:(id)sender {
NSUInteger elementsInArray = [newFileWindowControllerArray count];
NSLog(#"self=%p, array=%p", self, newFileWindowControllerArray);
[documentController newDocument:sender];
// and comment this out, for now:
// [[newFileWindowControllerArray objectAtIndex:i]close];
// instead...
NSLog(#"danh thinks my array will be ok: %#", newFileWindowControllerArray);
}
You probably do not mean to create another view controller on every button press, but I'm not sure what function you do want. Maybe you want an array of views? (To create many view controllers under the control of another, you'll want to read up on container view controllers, here).

How to efficiently flatten a NSOutlineView?

I have a NSOutlineView which is binded to my NSTreeController. The content of the NSTreecontroller (myTreeController) is set with data using the command:
[self.myTreeController setContent:self.myArrayOfFiles];
The content of the array consists of NSTreeNode parent and children objects that are added using a NSTreeNode subclass (TKnode):
[TKnode treeNodeWithRepresentedObject:myRootObject].
This works very well and displays my NSoutlineView correctly. But when I want to iterate over the content of the NSOutlineView I need to flatten it and store the objects in an array. This is where I could need some help as the solution I have is not optimal and potentially prone to errors.
I first return the content of the NSTreeController using:
- (NSArray *)rootNodes;
{
return [[myTreeController arrangedObjects] childNodes] ;
}
This returns a NSTreeControllerTreeNode array of size one that holds the tree structure. I then access the first layer of the tree structure using the childNodes method.
- (NSArray *)flattenedNodes
{
NSMutableArray *mutableArray = [NSMutableArray array];
for (TKnode *rootnode in [self rootNodes]){
for (TKnode *node in [rootnode childNodes]){
if (![[node representedObject] isLeaf]){
[mutableArray addObjectsFromArray:[self descendants:[node representedObject]]];
}
else {
[mutableArray addObject:[[node representedObject] representedObject]];
}
}
}
DLog(#"My files: %lu",[mutableArray count]);
return [[mutableArray copy] autorelease];
}
The children of the children are accessed recursively using the following method:
- (NSArray *)descendants:(TKnode *) node
{
NSMutableArray *descendantsArray = [NSMutableArray array];
for (TKnode *mynode in [node childNodes]) {
[descendantsArray addObject:[mynode representedObject]];
if (!mynode.isLeaf){
[descendantsArray addObjectsFromArray:[self descendants:mynode]];
}
}
return [[descendantsArray copy] autorelease]; // return immutable
}
This works and returns an array containing all of my objects, not the NSTreeNodes, but the objects I am interested in. But it seems error prone that I have to call representedObject sometimes twice on an object to access my object. Is this the way I am supposed to work with NSTReeController and NSTreeNodes? Is there a better way? The reason I wanted to use NSTreeNode and NSTreeController in the first place was to get some of the already implemented methods for free such as sorting and arrangedObjects, which I am used to with NSTableView. But the method I use to access the NSTreeController content does not seem correct ? Should I use the arrangedObjects to get the content of the NSTReeController or should I use a different approach? Any suggestions for how to correctly flatten a NSOutlineView is highly appreciated.
Thanks! Cheers, Trond
This question turned out to be a real tumbleweed (my second in one month) so I wanted to follow up with some more info. I ended up using the above posted code for flattening my NSOutlineView as I was unable to find a better option. When you have to recursively iterate through an unknown number of subfolders (in my case) this seems to be the best option where the descendants method is called for each time you reach a deeper level. I found a very useful website with a number of useful NSTreeController extensions here, which uses the same approach as I had taken. Unless someone helps me figure out a faster and better algorithm for flattening the arrangedObjects of a NSTreeController, I believe i will stick with this approach.
Cheers, Trond
Update: After a question for the solution I decided to post my method for how to flatten an outlineview. I hope his can help others.
- (void) flattenOutlineview
{
NSMutableArray *nodeArray = [NSMutableArray array];
[self.myFlattenedFiles removeAllObjects];
[self.myFlattenedNodes removeAllObjects];
[self.myIndexDict removeAllObjects];
[nodeArray removeAllObjects];
for (SDNode *rootnode in self.rootNodes)
{
[nodeArray addObject:rootnode];
[self.myIndexDict setObject:[rootnode indexPath]
forKey:[[[rootnode representedObject] representedObject] fullpathL]];
for (SDNode *node in [rootnode childNodes])
{
if (node.isLeaf){
[nodeArray addObject:node];
[self.myIndexDict setObject:[node indexPath]
forKey:[[[node representedObject] representedObject] fullPathCopy]];
}
else {
[nodeArray addObjectsFromArray:[self descendants:node]];
[self descendantsIndex:node];
}
}
}
[self.myFlattenedNodes setArray:[nodeArray copy]];
for (SDNode *node in self.myFlattenedNodes)
{
[self.myFlattenedFiles addObject:[[node representedObject] representedObject]];
}
}
Saw this linked from another answer I just wrote. Provided you have everything in the tree expanded/displayed this would work:
for (NSUInteger row = 0; row < [outlineView numberOfRows]; row++)
{
NSTreeNode* node = [outlineView itemAtRow:row];
[nodeArray addObject:node];
}
The rows of the outlineView are basically a flattened tree. And you could perhaps expand every item while iterating to get them all.

NSArrayController : removeAllObjects does not refresh TableView

In my application I add objects directly to an ArrayController. When I want to clean all items I do:
[[downloadItemsController content] removeAllObjects];
This command however does not refresh the TableView the arraycontroller is bound to. If I remove all items and add another new items I only see that item. That is fine but if I don't add anything I still have all my items in the table.
If I do
[downloadItemsController prepareContent];
all old items are removed from the tableview but than I will get an new and empty item I can edit. I don't want that and because one of my columns has a checkboxcell I always get an row with a checkbox.
I just need an empty table with no items after I remove all existing items.
To quickly remove all the objects from an NSArrayController object:
NSRange range = NSMakeRange(0, [[anArrayController arrangedObjects] count]);
[anArrayController removeObjectsAtArrangedObjectIndexes:[NSIndexSet indexSetWithIndexesInRange:range]];
The bindings should update automatically. See original SO answer.
This is because you're modifying the controller's content "behind its back." Try using the array controller's -removeObjects: method.
This worked for me
[anArrayController setContent:nil]
You can start populating its contents right away like this
[anArrayController addObject: #{ .... }]
[anArrayController addObject: #{ .... }.mutablecopy]
[anArrayController addObject: aDictionary]
Calling removeObject.. group of methods is particularly inconvenient for a collection of Core Data objects. It seems that remove... is meant for those cases when we really need to get rid of the data (like when user press 'Remove' button at UI), so the NSArrayController will try it's best to remove objects from Core Data DB too.
I have simple manual control of NSArrayController and I found that setContent: method works great for adding, removing and replacing the array of objects inside it. And just to clear the contents setContents:#[] can be used.
Sometimes when you remove is the key; here I defer using a volatile bool (undoInProgress) in a revert action, using solution above to flush a change array which observing method ignores while undo is in progress:
volatile bool undoInProgress = 0;
- (IBAction)revert:(id)sender
{
NSUserDefaults * prefs = [NSUserDefaults standardUserDefaults];
NSArray * undo = [NSArray arrayWithArray:changes];
undoInProgress = YES;
// We have a single key/value pair but do so in reverse order
for (NSInteger i=[undo count]-1; i>=0; --i)
{
NSDictionary * change = [undo objectAtIndex:i];
NSString * key = [change objectForKey:#"keyPath"];
id val = [change objectForKey:NSKeyValueChangeOldKey];
[prefs setValue:val forKey:key];
}
dispatch_async(dispatch_get_main_queue(), ^{
NSRange range = NSMakeRange(0, [[changeController arrangedObjects] count]);
[changeController removeObjectsAtArrangedObjectIndexes:[NSIndexSet indexSetWithIndexesInRange:range]];
undoInProgress = NO;
});
}