Re: Adding items to an NSOutlineView
Re: Adding items to an NSOutlineView
- Subject: Re: Adding items to an NSOutlineView
- From: Chris Hanson <email@hidden>
- Date: Tue, 14 Oct 2003 17:27:56 -0500
On Tuesday, October 14, 2003, at 04:23 PM, Darrin Cardani wrote:
If I passed the actual root object to -reloadItem:, I suspect it would
just raise a bad param exception, like when I passed it nil. (But I
have to admit, I haven't actually tried that.)
Actually, it shouldn't, it should do the right thing for the reload.
There's a pattern I use to make NSOutlineView easier to use. I create
two controller classes: OutlineViewDataSource and OutlineItem.
OutlineViewDataSource is a class that you instantiate in your nib file
and set as the data source for your outline view. It keeps track of a
root OutlineItem object, and handles passing the data source messages
from NSOutlineView on to the appropriate item. For example:
- (OutlineItem *)realItemForItem:(id)anItem
{
if (anItem == nil) {
return [self rootItem];
}
return anItem;
}
- (int)outlineView:(NSOutlineView *)anOutlineView
numberOfChildrenOfItem:(id)anItem
{
OutlineItem *realItem = [self realItemForItem:anItem];
return [realItem numberOfChildren];
}
OutlineItem is a base class that you subclass for each particular kind
of object you display. The base class includes all of the
functionality for keeping track of child items, etc. The reason this
is necessary is that NSOutlineView doesn't actually retain items that
you pass it; you need to keep track of them yourself.
The OutlineItem class generally has a method that's used to create a
child item, which must be overridden by a subclass to create a
particular kind of child item for a particular kind of object being
displayed. It also has a method that gets overridden to return whether
it can have child items (isExpandable).
All this probably sounds a lot more complicated than it is.
-- Chris
--
Chris Hanson, bDistributed.com, Inc. | Email: email@hidden
Custom Mac OS X Development | Phone: +1-847-372-3955
http://bdistributed.com/ | Fax: +1-847-589-3738
http://bdistributed.com/Articles/ | Personal Email: email@hidden
_______________________________________________
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.