Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

rdyornot

macrumors newbie
Original poster
Sep 15, 2007
28
3
I'm having trouble getting NSTableViews to load data. I've written a small sample program to try and figure out what i'm doing wrong, but I'm completely stumped. The code is posted below, can anyone point out what I'm doing wrong? There are 4 files for a sandbox class and an app delegate:

Code:
#import <Cocoa/Cocoa.h>
@interface SandBox : NSObject 
{
	NSString * sString;
	NSNumber * sNumber;
	NSString * bString;
}
@property (copy, nonatomic) NSString * sString, * bString;
@property (retain, nonatomic) NSNumber * sNumber;
@end

#import "SandBox.h"
@implementation SandBox
@synthesize sString, bString, sNumber;
- (id) init
{
	[super init];
	sString = [[NSString alloc] initWithString:@""];
	bString = [[NSString alloc] initWithString:@""];
	sNumber = [[NSNumber alloc] initWithInt:0];
	return self;
}
- (void) dealloc
{
	[sString release];
	[bString release];
	[sNumber release];
	[super dealloc];
}
@end

#import <Cocoa/Cocoa.h>
@class SandBox;
@interface SandBoxAppDelegate : NSObject <NSTableViewDataSource>
{
	NSArray * array;
	SandBox * box1, * box2, * box3;
	IBOutlet NSTableView * table;
}
- (void) prepareBoxesArray;
@end

#import "SandBoxAppDelegate.h"
#import "SandBox.h"
@implementation SandBoxAppDelegate
- (id) init 
{
	[super init];
	[self prepareBoxesArray];

	[table setDataSource:self];
	return self;
}
- (NSInteger) numberOfRowsInTableView:(NSTableView *)tv
{
	return [array count];
}
- (id) tableView:(NSTableView *)tv
objectValueForColumn:(NSTableColumn *)column
			 row:(NSInteger) rowIndex
{
	SandBox * box = [array objectAtIndex:rowIndex];
	return [box valueForKey:[column identifier]];
}
- (void) dealloc
{
	[box1 release];
	[box2 release];
	[box3 release];
	[array release];
	[super dealloc];
}
- (void) prepareBoxesArray
{
	box1 = [[SandBox alloc] init];
	[box1 setSNumber:[NSNumber numberWithInt:1]];
	[box1 setSString:@"sStringOne"];
	[box1 setBString:@"bStringOne"];
	box2 = [[SandBox alloc] init];
	[box2 setSNumber:[NSNumber numberWithInt:2]];
	[box2 setSString:@"sStringTwo"];
	[box2 setBString:@"bStringTwo"];
	box3 = [[SandBox alloc] init];
	[box3 setSNumber:[NSNumber numberWithInt:3]];
	[box3 setSString:@"sStringThree"];
	[box3 setBString:@"bStringThree"];
	array = [[NSArray alloc] initWithObjects:box1,box2,box2,nil];
}
@end
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
[table setDataSource:self]; is being called too early. Call it inside awakeFromNib, not init, because at init the nib hasn't loaded so table is nil. Or just connect it in Interface Builder and remove a line of code ;)
 

rdyornot

macrumors newbie
Original poster
Sep 15, 2007
28
3
It does. Then I get a message about having an illegal data source, but I can't find anything wrong with it myself.
 

rdyornot

macrumors newbie
Original poster
Sep 15, 2007
28
3
The complete error message is:

*** Illegal NSTableView data source (<SandBoxAppDelegate: 0x2000b2920>). Must implement numberOfRowsInTableView: and tableView:eek:bjectValueForTableColumn:row:

The column identifiers are: sString, bString, and sNumber (the properties of a SandBox object).
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.