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