sharkyenergy
sharkyenergy

Reputation: 4183

bind NSTableViewColumn programmatically

how can I programmatically bind a NSTableViewColumn to a NSArray.arrangedObjects key? I know how to do it in IB but was not able to find how its done programmatically. The code is adding new columns on Runtime, and these new columns need to be bound to my arraycontroller. i can't do it in IB because they do not yet exist in IB.

thanks!

Upvotes: 3

Views: 910

Answers (1)

ipmcc
ipmcc

Reputation: 29946

FWIW: I'm assuming you're using View-based NSTableViews here.

To do this, you need to have a delegate for the NSTableView. In that delegate you need to implement -tableView:viewForTableColumn:row:. In there, you can get the Table Cell View by asking the TableView for it. Once you have that, you have to snarf out the controls you want to bind, and then bind them. Once everything's hooked up, return the table cell view. Here's a trivial example:

- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
    NSView* retVal = [tableView makeViewWithIdentifier:[tableColumn identifier] owner:[tableView delegate]];

    // Note: You probably don't want to be snarfing out controls this way -- a better
    // way might be to have a custom NSTableCellView subclass with the controls plugged
    // into IBOutlets on it, but that's left as an exercise for the reader.
    NSTextField* textField = [[retVal subviews] objectAtIndex: 0];

    [textField bind: NSValueBinding toObject: retVal withKeyPath: @"objectValue.name" options: nil];

    return retVal;
}

Upvotes: 5

Related Questions