NSTableView

Discussion in 'Mac Programming' started by rdyornot, Jul 21, 2010.

  1. rdyornot macrumors newbie

    Joined:
    Sep 15, 2007
    #1
    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
     
  2. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    [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 ;)
     
  3. rdyornot thread starter macrumors newbie

    Joined:
    Sep 15, 2007
    #3
    Thanks - I tried it both ways, still not working :-/
    -rc
     
  4. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #4
    Does your numberOfRowsInTableView: method get called?
     
  5. rdyornot thread starter macrumors newbie

    Joined:
    Sep 15, 2007
    #5
    It does. Then I get a message about having an illegal data source, but I can't find anything wrong with it myself.
     
  6. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
  7. rdyornot thread starter macrumors newbie

    Joined:
    Sep 15, 2007
    #7
    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).
     
  8. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #8
    objectValueForColumn != objectValueForTableColumn :)
     
  9. rdyornot thread starter macrumors newbie

    Joined:
    Sep 15, 2007

Share This Page