dynamically adding and binding NSTableColumns
dynamically adding and binding NSTableColumns
- Subject: dynamically adding and binding NSTableColumns
- From: "Jonathan 'Wolf' Rentzsch" <email@hidden>
- Date: Sun, 7 Nov 2004 09:45:47 -0600
I have a columnar data model. That is, while each column always has the
same number of rows as its peer columns, other than that each column is
rather stand-alone.
I'm creating my columns at runtime based on user-entered data. I thought
I could just allocate NSTableColumns as I need them, create an
NSArrayController and bind them up.
This works when I have just one column, but fails as I add more.
Here's the repro, with the entire compiled project at
<http://rentzsch.com/share/DynamicTableColumnSpike.zip> (60K):
Imagine an empty (NSTableColumn-less) NSTableView, bound to an IBOutlet
named `tableView`. Here's my code to dynamically add a column:
- (IBAction) addColumnAction:(id)sender {
// Create and add a column.
NSTableColumn *column = [[[NSTableColumn alloc] initWithIdentifier:nil]
autorelease];
[tableView addTableColumn:column];
// Create an array controller, bind it to sample data
// and then bind the column to the controller.
NSArrayController *controller = [[[NSArrayController alloc] init]
autorelease];
[controller setContent:[NSArray arrayWithObjects:@"one", @"two",
@"three", nil]];
[column bind:@"value" toObject:controller
withKeyPath:@"arrangedObjects" options:nil];
}
The first time you add a column, the NSTableView ends ups looking like
this:
+-------+
| Field |
+-------+
| one |
| two |
| three |
+-------+
But if you add another column, executing the same code, it ends up
looking like this:
+-------+-------------------+
| Field | Field |
+-------+-------------------+
| one | (one, two, three) |
| two | (one, two, three) |
| three | (one, two, three) |
+-------+-------------------+
And then continues on, as you add more columns:
+-------+-------------------+-------------------+
| Field | Field | Field |
+-------+-------------------+-------------------+
| one | (one, two, three) | (one, two, three) |
| two | (one, two, three) | (one, two, three) |
| three | (one, two, three) | (one, two, three) |
+-------+-------------------+-------------------+
What am I doing wrong? I suspect it lies on the fact I'm using multiple
NSArrayControllers with a single NSTableView. Usually you just use one
across multiple NSTableColumns, relying on the binding's key path to get
specific data into a column.
| Jonathan 'Wolf' Rentzsch http://rentzsch.com
| Red Shed Software http://redshed.net
| "better" necessarily means "different"
_______________________________________________
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