Bind NSTableViewColumn programmatically

How can I programmatically bind the NSTableViewColumn to the NSArray.arrangedObjects key? I know how to do this in IB, but couldn't find how to do it programmatically. The code adds new columns at Runtime and these new columns should be bound to my array controller. I can't do this in IB because they don't exist in IB yet.

thank!

+3


source to share


1 answer


FWIW: I am assuming you are using NSTableView

View based.

To do this, you need to have a delegate for NSTableView

. In this you need to implement -tableView:viewForTableColumn:row:

. There you can get a Table Cell View by asking a TableView for it. After that, you have to rip out the controls that you want to bind and then bind them. Once everything is connected, 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 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;
}

      

+5


source







All Articles