Re: Saving state of NSOutlineView
Re: Saving state of NSOutlineView
- Subject: Re: Saving state of NSOutlineView
- From: Emma Whan <email@hidden>
- Date: Wed, 11 Jun 2003 09:54:07 +1000
You don't have to return a string from
outlineView:persistentObjectForItem: In fact, since the documentation
for these methods suggests you should return an archived object, that
is what I have always done. What I do is archive the item itself in
persistentObjectForItem and then unarchive it in
itemForPersistentObject. The object does have to be unique for every
item in your datasource. The process will only work if isEqual is
implemented in the objects (for reasons explained below). So if you
are archiving items of a custom class, make sure you implement isEqual
in that class first. Obviously with a custom class, you also need to
implement encodeWithCoder and initWithCoder.
The way that autosaveExpandedItems actually works is that every time
you expand an item, a copy of that item is archived in [NSUserDefaults
standarduserDefaults]. When you relaunch the application, it checks
every item in the outlineView and compares it with the ones it has
saved in the standardUserDefaults. If there is a match, it expands the
item. Because it needs to check for matches, isEqual has to be
implemented or none of your items will be expanded.
My code for implementing persistentObjectForItem and
objectForPersistentItem is simply as follows:-
- (id)outlineView:(NSOutlineView *)outlineView
persistentObjectForItem:(id)item
{
return [NSKeyedArchiver archivedDataWithRootObject:item];
}
- (id)outlineView:(NSOutlineView *)outlineView
itemForPersistentObject:(id)object
{
return [NSKeyedUnarchiver unarchiveObjectWith
Data:object];
}
Saving NSStrings instead of archived objects would probably work (I
haven't tried it) but each string will need to be unique and your
methods would obviously need to be able to determine what string
belongs to what object. The difficulty with using strings is that it
is quite likely that two items might return the same string. You need
to come up with some way of ensuring that each object can be uniquely
identified. This is the same even if you use archived objects so that
isEqual will only return true if the exact same object is unarchived.
I use timestamps for this purpose. Whenever I create an object in my
datasource, I give that object an NSTimeInterval variable representing
the exact submillisecond it was created. isEqual then compares the
timestamps.
I suppose I could have just returned the timestamp itself (or an
NSNumber object representing it) in the persistentObjectForItem method.
But if I did that, in the itemForPersistentObject method I would have
to loop recursively through every single item in my datasource until I
found the item with the matching timestamp.
On Tuesday, June 10, 2003, at 03:00 PM,
email@hidden wrote:
>
I've found this post on saving the state of the NSOutlineView:
>
>
When I'm creating my dataSource object:
>
>
[outline setAutosaveName:@"Subscriptions Outline"];
>
[outline setDataSource:dataSource];
>
[outline setDelegate:dataSource];
>
[outline setAutosaveExpandedItems:YES];
>
>
In the data source:
>
>
- (id)outlineView:(NSOutlineView *)outlineView
>
persistentObjectForItem:(id)item {
>
//return a string representing item
>
}
>
>
- (id)outlineView:(NSOutlineView *)outlineView
>
itemForPersistentObject:(id)object {
>
//return the item represented by object, the string returned
>
earlier
>
}
>
>
And it's that simple.
>
>
>
But to me, its still not that simple. there's still al little piece
>
missing. The author states that you must return a string representing
>
the item in the first and then the item represented by the string in
>
the second.
>
>
But its not clear what is actually going on. Am I supposed to simply
>
return some arbitraty string? A unique string for each item? How does
>
this string link with the actual item being collapsed or expanded.
>
Should it be the content of said item or just a unique name? Or perhaps
>
am I supposed to return a unique string and when this string is called,
>
return the object it corresponds to in my datasource?
>
>
I just need a bit of info to understand when this is called... perhaps
>
a simple code example... I feel I'm not too far but it doesn't compute
>
and I notice a lot of others have asked this.
>
>
Anybody have some sample code of an NSOutline which saves its state
>
after a save?
>
>
Thanks for any pointers
>
>
- Renaud
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.