Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically hiding columns in a NSTableView

Tags:

macos

cocoa

I want to dynamically hide/show some of the columns in a NSTableView, based on the data that is going to be displayed - basically, if a column is empty I'd like the column to be hidden. I'm currently populating the table with a controller class as the delegate for the table.

Any ideas? I see that I can set the column hidden in Interface Builder, however there doesn't seem to be a good time to go through the columns and check if they are empty or not, since there doesn't seem to be a method that is called before/after all of the data in the table is populated.

like image 526
Andy Avatar asked Dec 05 '25 06:12

Andy


2 Answers

In Mac OS X v10.5 and later, there is the setHidden: selector for NSTableColumn.

This allows columns to be dynamically hidden / shown with the use of identifiers:

NSInteger colIdx;
NSTableColumn* col;

colIdx = [myTable columnWithIdentifier:@"columnIdent"];
col = [myTable.tableColumns objectAtIndex:colIdx];
[col setHidden:YES];
like image 99
Cameron Hotchkies Avatar answered Dec 07 '25 18:12

Cameron Hotchkies


I've done this with bindings, but setting them up programmatically instead of through Interface Builder.

This psuedo-snippet should give you the gist of it:

NSTableColumn *aColumn = [[NSTableColumn alloc] initWithIdentifier:attr];
[aColumn setWidth:DEFAULTCOLWIDTH];
[aColumn setMinWidth:MINCOLWIDTH];
[[aColumn headerCell] setStringValue:columnLabel];

[aColumn bind:@"value"
     toObject:arrayController 
  withKeyPath:keyPath 
  options:nil];             

[tableView addTableColumn:aColumn];
[aColumn release];

Of course you can add formatters and all that stuff also.

like image 39
amrox Avatar answered Dec 07 '25 19:12

amrox



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!