add new NSTreenode to NSTreecontroller
add new NSTreenode to NSTreecontroller
- Subject: add new NSTreenode to NSTreecontroller
- From: Alex da Franca <email@hidden>
- Date: Mon, 11 Oct 2010 16:23:38 +0200
Hello list,
first of all, the usual newbie disclaimer blurb... As You will see
quickly I just started with objective-c and cocoa
Now the problem:
I have set up a NSOutlineView with an NSTreecontroller as its
datasource. (did the binding in IB)
After some initial problemes I got it to work, or at least that it
looks like it is working.
I can display data, interact with it and delete a selected node.
But for the life of me, I can figure out how to add a new node :-(
In order to display a whole new Outlineview I use the setContent
message of NSTreecontroller and pass in a NSMutableArray which
contains a number of NSTreenode which were created using
treeNodeWithRepresentedObject, where the object is a simple custom
datatype, which serves the values. All this works fine, if I create
and populate the array and then setContent of NSTreecontroller.
But, if I create the very same way a new NSTreenode, which I then add
to the NSTreecontroller, the following happens:
a new EMPTY line WITH disclosure triangle appears!If I open the
disclosure triangle there are no items, although they should be.
So of course I thought, that something is wrong with the Treenode or
the custom object, that the custom object doesn't respond correctly,
if it is "asked" by the Outlineview for the values for the columns.
But since it works in the first place, I thought, that the objects had
been released and therefore deallocated. So I added "retains" "all
over the place" in the quest of finding the culprit. To no avail.
I already spend days googling and trying... :-(
All I found out, was that the NSTreecontroller is supposed to be buggy
and badly designed :-(
I can't believe, that it still is buggy and if so, that these bugs are
so severe, that a simple function like "add" wouldn't work.
I am pretty sure, that it is something I do completely wrong :-(
here is the code to display the entire Outline view, which seems to
work OK:
{code}
- (void)setApplicableFilesData:(NSMutableArray *)newData
{
// newData is a simple array with pathnames to folders
NSEnumerator *foldersEnum = [newData objectEnumerator];
NSEnumerator *filesEnum;
NSString *thisFolder;
NSDictionary *thisFile;
NSMutableArray *applicableFilesArray;
TreeDatum *td;
NSMutableArray *roots = [NSMutableArray array];
while (thisFolder = [foldersEnum nextObject])
{
td = [TreeDatum treeDatumFromName:[thisFolder
lastPathComponent] value:thisFolder srcname:[thisFolder
lastPathComponent] srcvalue:thisFolder];
NSTreeNode *inode = [NSTreeNode
treeNodeWithRepresentedObject:td];
applicableFilesArray = [xmlutilsObj
getListOfApplicableFiles:thisFolder]; // get an array of
NSDictionaries with appropriate infos for each file
if (![applicableFilesArray count])
{
td = [TreeDatum treeDatumFromName:NSLocalizedString(@"- no
applicable files -", @"List item if no applicable files were found in
the actionfolder") value:@"-" srcname:@"-" srcvalue:@"-"];
NSTreeNode *jnode = [NSTreeNode treeNodeWithRepresentedObject:td];
[[inode mutableChildNodes] addObject:jnode];
}
else
{
filesEnum = [applicableFilesArray objectEnumerator];
while (thisFile = [filesEnum nextObject])
{
td = [TreeDatum treeDatumFromName:[thisFile
objectForKey:@"filename"] value:[thisFile objectForKey:@"fullpath"]
srcname:[thisFile objectForKey:@"sourceFileName"] srcvalue:[thisFile
objectForKey:@"sourceFilePath"]];
NSTreeNode *jnode = [NSTreeNode treeNodeWithRepresentedObject:td];
[[inode mutableChildNodes] addObject:jnode];
}
}
[roots addObject:inode];
}
[applicableFilesTreeController setContent:roots];
}
{code}
now in order to add a new node my code just does the same like the
above code except, that it doesn't append the created node to the
mutable array, which is set at the end using setContent, but rather
this node is added directly to the Treecontroller (->
applicableFilesTreeController).
That doesn't work :-(
{code}
- (void)addPathForActionFolder:(NSString *)fname
{
// fname is a path to a folder
TreeDatum *td = [TreeDatum treeDatumFromName:[fname
lastPathComponent] value:fname srcname:[fname lastPathComponent]
srcvalue:fname];
NSTreeNode *inode = [NSTreeNode treeNodeWithRepresentedObject:td];
NSMutableArray *applicableFilesArray = [xmlutilsObj
getListOfApplicableFiles:fname];
NSEnumerator *filesEnum;
NSDictionary *thisFile;
if (![applicableFilesArray count])
{
td = [TreeDatum treeDatumFromName:NSLocalizedString(@"- no
applicable files -", @"List item if no applicable files were found in
the actionfolder") value:@"-" srcname:@"-" srcvalue:@"-"];
NSTreeNode *jnode = [NSTreeNode treeNodeWithRepresentedObject:td];
[[inode mutableChildNodes] addObject:jnode];
}
else
{
filesEnum = [applicableFilesArray objectEnumerator];
while (thisFile = [filesEnum nextObject])
{
td = [TreeDatum treeDatumFromName:[thisFile
objectForKey:@"filename"] value:[thisFile objectForKey:@"fullpath"]
srcname:[thisFile objectForKey:@"sourceFileName"] srcvalue:[thisFile
objectForKey:@"sourceFilePath"]];
NSTreeNode *jnode = [NSTreeNode treeNodeWithRepresentedObject:td];
[[inode mutableChildNodes] addObject:jnode];
}
}
[applicableFilesTreeController add:inode];
}
{code}
and here is the TreeDatum object:
{code}
#import <Cocoa/Cocoa.h>
@interface TreeDatum : NSObject {
NSString *name;
NSString *value;
NSString *srcname;
NSString *srcvalue;
}
@property (copy) NSString *name;
@property (copy) NSString *value;
@property (copy) NSString *srcname;
@property (copy) NSString *srcvalue;
+(TreeDatum*)treeDatumFromName:(NSString*)theName value:
(NSString*)theValue srcname:(NSString*)theSrcname srcvalue:
(NSString*)theSrcvalue;
@end
#import "TreeDatum.h"
@implementation TreeDatum
-(void) dealloc
{
[name release];
[value release];
[srcname release];
[srcvalue release];
[super dealloc];
}
@synthesize name;
@synthesize value;
@synthesize srcname;
@synthesize srcvalue;
+(TreeDatum*)treeDatumFromName:(NSString*)theName value:
(NSString*)theValue srcname:(NSString*)theSrcname srcvalue:
(NSString*)theSrcvalue
{
TreeDatum *td = [[[TreeDatum alloc] init] autorelease];
td.name = theName;
td.value = theValue;
td.srcname = theSrcname;
td.srcvalue = theSrcvalue;
return td;
}
@end
{code}
Many thanks in advance
Best regards
------------------- Alexander da Franca ----------------
Multimedia Developer |_________________________| Lingo Programmer
------------------- <http://www.farbflash.org> ----------------
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden