<p>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.</p>

<p>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.</p>

[{'answer_id': 69322, 'author': 'Matthew Schinckel', 'author_id': 188, 'author_profile': 'https://Stackoverflow.com/users/188', 'pm_score': 1, 'selected': False, 'text': "<p>I don't have a complete answer at this time, but look into Bindings.  It's generally possible to do all sorts of things with Cocoa Bindings.</p>\n\n<p>There's no Visibility binding for NSTableColumn, but you may be able to set the width to 0.</p>\n\n<p>Then you can bind it to the Null Placeholder, and set this value to 0 - but don't forget to set the other Placeholders to reasonable values.</p>\n\n<p>(As I said, this is just a start, it might need some tweaking).</p>\n"}, {'answer_id': 71553, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': "<p>There is no one time all the data is populated.  NSTableView does not store data, it dynamically asks for it from its data source (or bound-to objects if you're using bindings).  It just draws using data it gets from the data source and ditches it.  You shouldn't see the table ask for data for anything that isn't visible, for example.</p>\n\n<p>It sounds like you're using a datasource?  When the data changes, it's your responsibility to call -reloadData on the table, which is a bit of a misnomer.  It's more like 'invalidate everything'.  </p>\n\n<p>That is, you should already know when the data changes.  That's the point at which you can compute what columns should be hidden.</p>\n"}, {'answer_id': 71609, 'author': 'amrox', 'author_id': 4468, 'author_profile': 'https://Stackoverflow.com/users/4468', 'pm_score': 2, 'selected': False, 'text': '<p>I\'ve done this with bindings, but setting them up programmatically instead of through Interface Builder.</p>\n\n<p>This psuedo-snippet should give you the gist of it:</p>\n\n<pre><code>NSTableColumn *aColumn = [[NSTableColumn alloc] initWithIdentifier:attr];\n[aColumn setWidth:DEFAULTCOLWIDTH];\n[aColumn setMinWidth:MINCOLWIDTH];\n[[aColumn headerCell] setStringValue:columnLabel];\n\n[aColumn bind:@"value"\n     toObject:arrayController \n  withKeyPath:keyPath \n  options:nil];             \n\n[tableView addTableColumn:aColumn];\n[aColumn release];\n</code></pre>\n\n<p>Of course you can add formatters and all that stuff also.</p>\n'}, {'answer_id': 88734, 'author': 'Andy', 'author_id': 3857, 'author_profile': 'https://Stackoverflow.com/users/3857', 'pm_score': 0, 'selected': False, 'text': "<p>@amrox - If I am understanding your suggestion correctly, you're saying that I should bind a value to the hidden property of the NSTableColumns in my table? That seems like it would work, however I don't think that NSTableColumn has a hidden property, since the isHidden and setHidden messages control the visibility of the column - which tells me that this isn't a property, unless I'm missing something (which is quite possible).</p>\n"}, {'answer_id': 152558, 'author': 'Mecki', 'author_id': 15809, 'author_profile': 'https://Stackoverflow.com/users/15809', 'pm_score': 1, 'selected': True, 'text': "<p>A NSTable is just the class that paints the table. As you said yourself, you have some class you give the table as delegate and this class feeds the table with the data to display. If you store the table data as NSArray's within your delegate class, it should be easy to find out if one column is empty, isn't it? And NSArray asks your class via delegate method how many columns there are, so when you are asked, why not looking for how many columns you have data and report that number instead of the real number of columns you store internally and then when being asked for providing the data for (column,row), just skip the empty column.</p>\n"}, {'answer_id': 9490225, 'author': 'Cameron Hotchkies', 'author_id': 463465, 'author_profile': 'https://Stackoverflow.com/users/463465', 'pm_score': 4, 'selected': False, 'text': '<p>In Mac OS X v10.5 and later, there is the <a href="https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSTableColumn_Class/Reference/Reference.html#//apple_ref/occ/instm/NSTableColumn/setHidden:" rel="noreferrer"><code>setHidden:</code> selector</a> for NSTableColumn.</p>\n\n<p>This allows columns to be dynamically hidden / shown with the use of identifiers:</p>\n\n<pre><code>NSInteger colIdx;\nNSTableColumn* col;\n\ncolIdx = [myTable columnWithIdentifier:@"columnIdent"];\ncol = [myTable.tableColumns objectAtIndex:colIdx];\n[col setHidden:YES];\n</code></pre>\n'}, {'answer_id': 33037710, 'author': 'Daniel', 'author_id': 345258, 'author_profile': 'https://Stackoverflow.com/users/345258', 'pm_score': 2, 'selected': False, 'text': '<p>It does not work in the Interface Builder. However it works programatically. Here is how I bind a NSTableViewColumn with the identifier "Status" to a key in my NSUserDefaults:</p>\n\n<p><strong>Swift:</strong></p>\n\n<pre><code>tableView.tableColumnWithIdentifier("Status")?.bind("hidden", toObject: NSUserDefaults.standardUserDefaults(), withKeyPath: "TableColumnStatus", options: nil)\n</code></pre>\n\n<p><strong>Objective-C:</strong></p>\n\n<pre><code>[[self.tableView tableColumnWithIdentifier:@"Status"] bind:@"hidden" toObject:[NSUserDefaults standardUserDefaults] withKeyPath:@"TableColumnStatus" options:nil];\n</code></pre>\n'}, {'answer_id': 53111511, 'author': 'Lookaji', 'author_id': 4111774, 'author_profile': 'https://Stackoverflow.com/users/4111774', 'pm_score': 0, 'selected': False, 'text': '<p>I would like to post my solution updated for Swift 4 using Cocoa bindings and the actual <code>isHidden</code> flag without touching the column widths (as you might need to restore the original value afterwards...). Suppose we have a Checkbox to toggle some column visibility (or you can always toggle the <code>hideColumnsFlag</code> variable in the example below in any other way you like):</p>\n\n<pre><code>class ViewController: NSViewController {\n\n     // define the boolean binding variable to hide the columns and use its name as keypath\n     @objc dynamic var hideColumnsFlag = true\n\n     // Referring the column(s)\n     // Method 1: creating IBOutlet(s) for the column(s): just ctrl-drag each column here to add it\n     @IBOutlet weak var hideableTableColumn: NSTableColumn!\n     // add as many column outlets as you need...\n\n     // or, if you prefer working with columns\' string keypaths\n     // Method 2: use just the table view IBOutlet and its column identifiers (you **must** anyway set the latter identifiers manually via IB for each column)\n     @IBOutlet weak var theTableView: NSTableView! // this line could be actually removed if using the first method on this example, but in a real case, you will probably need it anyway.\n\n     // MARK: View Controller Lifecycle\n\n     override func viewDidLoad() {\n         super.viewDidLoad()\n\n         // Method 1\n         // referring the columns by using the outlets as such:\n         hideableTableColumn.bind(.hidden, to: self, withKeyPath: "hideColumnsFlag", options: nil)\n         // repeat for each column outlet.\n\n         // Method 2\n         // or if you need/prefer to use the column identifiers strings then:\n         // theTableView.tableColumn(withIdentifier: .init("columnName"))?.bind(.hidden, to: self, withKeyPath: "hideColumnsFlag", options: nil)\n         // repeat for each column identifier you have set.\n\n         // obviously use just one method by commenting/uncommenting one or the other.\n     }\n\n     // MARK: Actions\n\n     // this is the checkBox action method, just toggling the boolean variable bound to the columns in the viewDidLoad method.\n     @IBAction func hideColumnsCheckboxAction(_ sender: NSButton) {\n         hideColumnsFlag = sender.state == .on\n     }\n}\n</code></pre>\n\n<p>As you may have noticed, there is no way yet to bind the <code>Hidden</code> flag in Interface Builder as on XCode10: you can see the <code>Enabled</code> or <code>Editable</code> bindings, but only programmatically you will have access to the <code>isHidden</code> flag for the column, as it is called in Swift.</p>\n\n<p>As noted in comments, the second method relies on the column identifiers you must manually set either via Interface Builder on the Identity field after selecting the relevant columns or, if you have an array of column names, you can enumerate the table columns and assign the identifiers as well as the bindings instead of repeating similar code lines.</p>\n'}, {'answer_id': 68811090, 'author': 'Ruben Kazumov', 'author_id': 1543083, 'author_profile': 'https://Stackoverflow.com/users/1543083', 'pm_score': 0, 'selected': False, 'text': '<p>I found a straightforward solution for it.</p>\n<p>If you want to hide any column with the Cocoa binding technology:</p>\n<ol>\n<li>In your instance of the <code>NSArrayController,</code> create an attribute/parameter/slot/keyed value which will have <code>NSNumber</code> 0 if you want a particular column to be hidden and any value if not.</li>\n<li>Bind the table column object\'s <code>maxWidth</code> parameter to the data slot, described in (1). We will use the <code>maxWidth</code> bound parameter as a message receiver.</li>\n<li>Subclass the NSTableColumn:</li>\n</ol>\n<pre><code>import Cocoa\n\nclass Column: NSTableColumn {\n\n    /// Observe the binding messages\n    override func setValue(_ value: Any?, forKey key: String) {\n        if key == &quot;maxWidth&quot; &amp;&amp; value != nil { // Filters the signal\n            let w = value as! NSNumber // Explores change\n            if w == NSNumber(integerLiteral: 0) {\n                self.isHidden = true\n            } else {\n                self.isHidden = false\n            }\n            return // No propagation for the value change\n        }\n        super.setValue(value, forKey: key) // Propagate the signal\n    }\n    \n}\n\n</code></pre>\n<ol start="4">\n<li>Change the class of the column to <code>Column.</code></li>\n</ol>\n'}]
['https://Stackoverflow.com/questions/68821', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3857/']