Is there a trick to NSTableViews?
Is there a trick to NSTableViews?
- Subject: Is there a trick to NSTableViews?
- From: Dustin Robert Kick <email@hidden>
- Date: Mon, 12 Sep 2005 23:52:01 -0500
I am learning NSTableViews out of several sources, and I'm running
into illogical behavior from the code that I'm learning. The first
trouble I started having was with Learning Cocoa with Objective-C.
The code from that book has you create an object FoodItem, that
you're supposed to input into an NSTableView that you create with two
columns, one for "name", and one for "price". When I finished the
project and ran it, with no compiler errors, I got a blank tableView,
though I was able to select Cells in it that didn't show anything. I
then went to the book Cocoa Programming For Mac OS X, and built and
ran an NSTableView project that used an NSArrayController. This one
worked with no problems, though when an exercise said to rebuild the
project without the NSArrayController, I did, and it was similar to
the project in Learning Cocoa with Objective-C without the
NSArrayController, with some modifications, and sure enough, as I'd
feared, when it came time to test the program without the
NSArrayController, my program would insert objects that wouldn't show
up in the tableView that I'd built.
When I turned to the the web, I found two examples, one called
PODock, from http://www.projectomega.org/article.php?
lg=en&php=tuts_cocoa4&p=1 , that built and ran fine, and inserted
NSString objects well, but when I modified the code, to se e if maybe
this working project would show up my objects to insert, I could only
get compiler errors from the attempted insertion into the
NSTableView, where I'd been following every piece of advice and
teaching from the books that I came across.
Here are the errors
[Session started at 2005-09-12 23:08:23 -0500.]
2005-09-12 23:08:24.543 PODock2[7010] *** -[FoodItem copyWithZone:]:
selector not recognized [self = 0x350240]
2005-09-12 23:08:24.554 PODock2[7010] An uncaught exception was raised
2005-09-12 23:08:24.559 PODock2[7010] *** -[FoodItem copyWithZone:]:
selector not recognized [self = 0x350240]
2005-09-12 23:08:24.560 PODock2[7010] *** Uncaught exception:
<NSInvalidArgumentException> *** -[FoodItem copyWithZone:]: selector
not recognized [self = 0x350240]
PODock2 has exited due to signal 5 (SIGTRAP).
The final example that I came across is the one that really baffles
me, though, since I found one that does nothing different, as far as
I can see, from anything that I'd been doing, as far as object
insertion into the NSTableView, yet for some strange reason, this
example works. The one change that I saw, was that this example,
from http://www.cocoadevcentral.com/articles/000080.php put it's
objects', (Mailbox and Email) data into NSMutableDictionaries called
properties, and assigned them to keys and values. I went and changed
over every object in the other examples I'd been trying to make work
into this format, to see if it would make a difference, and it
didn't. For some strange reason, the code for MailDemo, inserting
objects into an NSTableView worked fine, and using the same technique
would not work for me if I would build a new, fresh project.
Well, at any rate, here is the code for the example from Learning
Cocoa with Objective-C, which seemed to be the most straightforward
example, and after reading the Apple Documentation and all the
documentation in all the books, and web site I could find, I
couldn't, and can't figure out why it isn't working the way it is put
into the computer straight off. The nib file is just simply a window
with an NSTextView in it, with the NSObject theDataSource chosen as
its datasource, and theDataSource is basically the controller class.
All the required methods are implemented, and they seem to be
implemented well, from what I can tell, but when I build and run, the
data doesn't show up, as in all the other projects. What is the
problem?
// FoodItem.h
#import <Cocoa/Cocoa.h>
@interface FoodItem : NSObject
{
NSString * name;
NSNumber * price;
}
-(NSString *)name;
-(NSNumber *)price;
-(void)setName:(NSString *)theName;
-(void)setPrice:(NSNumber *)thePrice;
@end
// FoodItem.m
#import "FoodItem.h"
@implementation FoodItem
-(id)init
{
[super init];
[self setName:@"New Item"];
[self setPrice:[NSNumber numberWithFloat:0.0]];
return self;
}
-(NSString *)name
{
return name;
}
-(NSNumber *)price
{
return price;
}
-(void)setName:(NSString *)theName
{
[theName retain];
[name release];
name=theName;
}
-(void)setPrice:(NSNumber *)thePrice
{
[thePrice retain];
[price release];
price=thePrice;
}
@end
//theDataSource.h
#import <Cocoa/Cocoa.h>
@interface theDataSource : NSObject
{
NSMutableArray * items;
IBOutlet NSTableView * tableView;
}
@end
//theDataSource.m
#import "theDataSource.h"
#import "FoodItem.h"
@implementation theDataSource
- (id)init
{
[super init];
// Some initial data for our interface
FoodItem * chimi = [[FoodItem alloc] init];
FoodItem * fajitas = [[FoodItem alloc] init];
[chimi setName:@"Chimichanga"];
[chimi setPrice:[NSNumber numberWithFloat:5.50]];
[fajitas setName:@"Fajitas"];
[fajitas setPrice:[NSNumber numberWithFloat:8.25]];
[items addObject:chimi];
[items addObject:fajitas];
[chimi release];
[fajitas release];
[tableView reloadData];
return self;
}
- (void) awakeFromNib
{
[tableView setBackgroundColor:[NSColor greenColor]];
[tableView setGridColor:[NSColor blackColor]];
}
- (int)numberOfRowsInTableView:(NSTableView *)aTableView
{
return [items count];
}
- (id)tableView:(NSTableView *)tableView
objectValueforTableColumn:(NSTableColumn *)tableColumn
row:(int)row
{
NSString * identifier = [tableColumn identifier];
FoodItem * item = [items objectAtIndex:row];
return [item valueForKey:identifier];
}
@end
Dustin Kick
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden