PDA

View Full Version : NSTableView - problems in paradise




hiddenpremise
Jan 24, 2009, 04:19 PM
Thank you eddie and kainjow. Problem solved ;)
_______________
I am trying to create an example of an NSTableView implementation.

The Xib file contains 1 table view, 1 text field, and 1 button appropriately connected. The app compiles and launches. Using breakpoints I have found that the array does indeed record data for every time you click the button, however the TableView specific methods never run. Any ideas?

Thanks so much for all of your help, past and present,
Whit

TableViewController.h
#import <Cocoa/Cocoa.h>

@interface TableViewController : NSObject/* Specify a superclass (eg: NSObject or NSView) */ {
IBOutlet NSTableView *tvTable;
IBOutlet NSTextField *txtTexToAdd;
NSMutableArray *arrayOfText;
}
- (IBAction)btnAdd_Click:(id)sender;
- (int)numberOfRowsInTableView:(NSTableView *)tableView;
- (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex;
@end


TableViewController.m
#import "TableViewController.h"

@implementation TableViewController
- (id) init
{
if (self = [super init])
{
arrayOfText = [[NSMutableArray alloc] init];
}
return (self);
}
- (void) dealloc
{
[arrayOfText release];
[super dealloc];
}

- (IBAction)btnAdd_Click:(id)sender
{
NSString *textToAdd = [txtTexToAdd stringValue];
[arrayOfText addObject:textToAdd];
[tvTable reloadData];
}

- (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView
{
return [arrayOfText count];
}
- (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex
{
NSString *valueToDisplay = [arrayOfText objectAtIndex:rowIndex];
return valueToDisplay;
}
@end



eddietr
Jan 24, 2009, 05:08 PM
I assume your TableViewController is set as the dataSource for your TableView?

hiddenpremise
Jan 24, 2009, 06:18 PM
Eddie, I'm not sure what you mean by set as datasource. I'm somewhat of a noob.

I control dragged from my TableViewController object in Interface builder to the tableview object in my window and set it to the tvTable item

I could email a zipped version of the project file

kainjow
Jan 24, 2009, 07:58 PM
Like he said you need to also go the opposite direction in IB to tell the table view that your class is the datasource. You only hooked up your class to the table to connect the table view outlet, but you need to go the other way so the table knows where to get its data from.

eddietr
Jan 24, 2009, 08:43 PM
Eddie, I'm not sure what you mean by set as datasource. I'm somewhat of a noob.

I control dragged from my TableViewController object in Interface builder to the tableview object in my window and set it to the tvTable item

I could email a zipped version of the project file

Ok, well as kainjow said you need two connections between your tableViewController and the TableView and here's why:

So the first connection is between your outlet called "tvTable" and the tableview. So when you make that connection in IB, that sets the tvTable instance variable to the id of the table view. That's how this line works:

[tvTable reloadData]

So you send the reloadData message to the object whose id is stored in "tvTable". (That id having been put there in your XIB/NIB).

But now you want your TableView to send messages to your controller to get its data. To get their data, tableviews send messages to a datasource whose id is stored in the tableview's instance variable called "dataSource".

So basically internally the TableView is making calls like:

[dataSource numberOfRowsInTableView:self]

much like your controller is calling [tvTable reloadData]

So just as your controller needs the id of the TableView stored in its "tvTable" var, likewise the TableView needs the id of your controller in its instance variable called "dataSource"

So that's why you need the two connections in IB. I hope that makes sense?