Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 979
sharkyenergy Avatar asked May 23 '26 04:05

sharkyenergy


1 Answers

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;
}
like image 135
ipmcc Avatar answered May 24 '26 17:05

ipmcc



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!