views:

377

answers:

1

What is an easy way to set up my NSTableView with multiple columns to only display certain data in one column. I have the IBOutlets set up, but I don't know where to go from there.

+1  A: 

Assuming you're not using Cocoa Bindings/Core Data, you can display data in an NSTableView by implementing two methods from the NSTableViewDataSource protocol. Typically your controller will implement the protocol, so open the controller .m file and add these methods to the controller's @implementation:

- (NSInteger)numberOfRowsInTableView:(NSTableView*)tableView {
  return 25;  // fill this out
}

– (id) tableView:(NSTableView*)tableView
       objectValueForTableColumn:(NSTableColumn*)column
       row:(int)row {
  return row % 3 ? @"Tick..." : @"BOOM!";  // fill this out
}

You need to set the table's dataSource property to the controller. In Interface Builder control-drag from the table view to the controller and set dataSource. Now build and run and you should see your data in the table.

If you only want to fill out one column, add an IBOutlet NSTableColumn* to your controller; let's call it explosiveColumn. In Interface Builder, control-drag from the controller to the column you want to fill in and set explosiveColumn. Then, in tableView:objectValueForTableColumn:row: you can test if the column parameter is the same object as the one that the outlet is set to:

– (id) tableView:(NSTableView*)tableView
       objectValueForTableColumn:(NSTableColumn*)column
       row:(int)row {
  if (column == explosiveColumn) {
    return row % 3 ? @"Tick..." : @"BOOM!";
  } else {
    // other columns blank for now
    return nil;
  }
}

This tutorial might be useful: http://www.cocoadev.com/index.pl?NSTableViewTutorial

Dominic Cooney
how can I test it? I have multiple columns, but I want data to only be shown in one of them
Matt S.
I've updated the answer with more specific details on this. Basically you inspect the column parameter to see if it is the one you're interested in, and return `nil` otherwise.
Dominic Cooney
thank you!!!!!!
Matt S.