NSTableView: data does not appear
NSTableView: data does not appear
- Subject: NSTableView: data does not appear
- From: Ngak Leng Sim <email@hidden>
- Date: Sun, 19 Jun 2005 18:17:58 -0700
Greetings,
Apologies for the long posting, but I'm having some trouble getting
values to show up in NSTableView, and I'm wondering if someone could
point out where I'm going wrong.
In my application, I have the following objects:
(1) IBOutlet NSTableView * table
(2) NSMutableArray * records (my data source, which is an array of
mutable dictionaries)
(3) An AppController (NSObject) which contains the mutable array "records"
I've implemented the necessary methods in AppController.m:
(1) numberOfRowsInTableView:tableView: and
(2) tableView:objectValueForTableColumns:row:.
The number of rows and columns in the NSTableView is correct, but the
data does not show up. My code in AppController.m is as follows:
-(id)init {
if (self = [super init]) {
records = [[NSMutableArray alloc] init];
// set some initial values so that something shows up in the
table when launched
NSArray * keys = [NSArray arrayWithObjects:@"key1", @"key2", nil];
NSArray * vals = [NSArray arrayWithObjects:[NSNumber
numberWithDouble:0.00], [NSNumber numberWithDouble:0.00], nil];
NSMutableDictionary * dict = [[NSMutableDictionary alloc]
initWithObjects:vals forKeys:keys];
int i;
for (i = 0; i < 10; i++) {
[records addObject:dict];
}
[dict release];
}
return self;
}
-(void)awakeFromNib {
[table setDataSource:self];
NSArray * keys = [[records objectAtIndex:0] allKeys];
int i;
for (i = 0; i < [keys count]; i++) {
NSTableColumn * tc = [[NSTableColumn alloc] init];
[tc setIdentifier:[keys objectAtIndex:i]];
[tc setEditable:TRUE];
// Set table header
NSCell * hdr = [[NSCell alloc] init];
[hdr setTitle:[tc identifier]];
[tc setHeaderCell:hdr];
[hdr release];
[table addTableColumn:tc]; // Add to table view
[tc release];
}
[table reloadData];
}
-(int)numberOfRowsInTableView:(NSTableView *)tableView
{
return [records count];
}
-(id)tableView:(NSTableView *)tableView
objectValueForTableColumns:(NSTableColumn *)aTableColumn
row:(int)rowIndex
{
NSMutableDictionary * dict = [records objectAtIndex:rowIndex];
id key = [aTableColumn identifier];
return [dict objectForKey:key];
}
On a related note, I had originally wanted to use bindings. In all the
examples I've seen about Cocoa bindings, the keys in the data source
are fixed and known, thus the programmer can use Interface Builder to
set the bindings to the table columns. In my case, the keys change
over time, and is set by the user. Can anyone point me to some
resource or examples that shows how one can programmatically bind
table columns to the data source when the keys are not fixed? That is,
what should I pass to the bind:toObject:forKeyPath:options: method?
I'm unable to figure out what to parameter I should pass as the
"forKeyPath" argument.
Any help is appreciated. Thanks in advance.
_______________________________________________
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