Re: Multiple NSTableView question
Re: Multiple NSTableView question
- Subject: Re: Multiple NSTableView question
- From: Brian Hill <email@hidden>
- Date: Thu, 21 Jun 2001 13:37:37 -0500
On Thursday, June 21, 2001, at 11:13 AM, Phillip Mills wrote:
- (int)numberOfRowsInTableView:(NSTableView *)theTableView
{
[theTableView doSpecialStuff];
[self doCommonStuff];
}
However, from a Model/View/Controller point of view, I'd ask yourself,
'What is the 'special stuff' that needs to be done? Is it in the realm
of the table view (ie., does it have solely to do with presenting the
data in the table), or is it more in the realm of the data model (ie.,
preparing the data so you can count the rows, etc...).
I agree completely. It touches on the idea of whether objects should be
designed based on behavior. If so, then the polymorphic approach makes
more
sense to me because it expresses the idea that the difference between
the
objects is *in* the objects.
Setting up the logic with multiple "if" statements expresses the idea
that
the objects don't encapsulate their differences, but rather are
differentiated by what some other object does to/with them.
Perhaps it's just a question of whether one is more comfortable with a
particular style.
Yes, but.
Is the 'behavior' of your table the manner in which is displays a column
and responds to clicks, keypresses, etc... or is it something to do with
what is *in* the table?
If it's something that's in the table (ie., *what* the table is showing,
not *how* it is showing it), then I generally tend to put that behavior
in the data model or controller, not the view.
As an example, the only time I've subclassed an NSTableView that I can
recall was to create one that would respond with the double click action
if a user pressed the Return or Enter key while it was selected.
However, I have a number of different types of table data source
classes, including one that uses a NSMutableArray of
NSMutableDictionaries as a data store. That particular class
(ArrayTableDataSource) *also* has a delegate that will be asked for
every column whether it would like to take over providing the data for
that column.
Then, I can re-use this pair (NSTableView & ArrayTableDataSource) all
over the place, and the delegate pattern I've implemented allows me to
even use it in cases where the data isn't a simple string (such as an
NSImage for an icon). My Controller class 'owns' an array data source
that provides data to the table. The data source has my Controller as
it's delegate. For the column that doesn't fit into the 'array of
dictionaries' data model (one with an icon instead of text) that the
data source is built around, the delegate (ie., my Controller) provides
the data.
That brings me to the one serious problem I always had with PowerPlant
(well, *one of* the serious problems). PowerPlant doesn't really have a
clue about MVC separation. The LOutlineView was a rampant example of
this problem (for one program I needed 12 subclasses of LOutlineView to
show 12 different types of data. Yuck).
About polymorphism and UI classes in Cocoa -- yes, it's all over the
place - in fact, since (generalization coming...) classes in ObjC are
sort of defined by the messages they respond to more than their
'ancestry' (at least in comparison to C++), there is a lot more
'informal polymorphism' in Cocoa than in PowerPlant.
This gets close to what I was almost asking. :-) Assuming that I want
to
keep making objects that are responsible for all (or most of) their own
behavior, what's the "more Cocoa than sub-classing" way of getting
there?
Specifically, what kind of a things (categories? protocols?) do I
create to
implement the various "doSpecialStuff" methods of my above example?
Here's an example of how to add the 'doSpecialStuff' method to
NSTableView using a category:
@interface NSTableView(MySpecialStuffCategory)
- (void)doSpecialStuff;
@end
@implementation NSTableView(MySpecialStuffCategory)
- (void)doSpecialStuff
{
//do something special
}
@end
Now, all the NSTableViews in your app will respond to the
'doSpecialStuff' method.
Brian
email@hidden
http://personalpages.tds.net/~brian_hill
___________________________________________________________
"Why? I came into this game for adventure - go anywhere, travel
light, get in, get out, wherever there's trouble, a man alone.
Now they've got the whole country sectioned off and you can't
move without a form. I'm the last of a breed."
-- Archibald "Harry" Tuttle, Rogue HVAC Repairman
___________________________________________________________