title: Programmatically adding columns (with bindings) to a view-based NSTableView?
body: <p>I've got a simple app that lists some models in a <code>NSTableView</code>.</p>
  
  <p>The table is a completely standard view-based table with a couple of columns, and is populated through an <code>NSArrayController</code> using bindings. All of that is set up in Interface Builder, and it works just as expected.</p>
  
  <p>Now, I'd like to add some more columns programmatically, and have these new columns bind to various keypaths in the models. And there, I'm stuck.</p>
  
  <p>For simplicity's sake, assume this setup:</p>
  
  <ol>
  <li>The Model objects are plain objects with just a single NSString property: <code>name</code></li>
  <li>The AppDelegate has an NSArray property (<code>models</code>) which holds a number of Model instances.</li>
  <li>The NSArrayController's <code>content</code> is bound to the application delegate's <code>models</code> array.</li>
  <li>The NSTableView's <code>content</code> is bound to the array controller's <code>arrangedObjects</code>.</li>
  <li>The table's first column is bound (in IB) to display the <code>name</code> property, i.e. column → table cell view → static text table view cell → <em>value</em> is bound to the table cell view's <em>objectValue.name</em></li>
  </ol>
  
  <p>This is, as far as I know, straightforward and by the book, and it works like a charm.</p>
  
  <p><strong>But...</strong> how do I add more columns, then?</p>
  
  <p>To add a column programatically (to keep it simple, let's just say this new column should <em>also</em> just display the <code>name</code> property, just like the column that's already there), I imagine I'd do something like this in the app delegate:</p>
  
  <pre><code>NSTableColumn* newColumn = [[NSTableColumn alloc] initWithIdentifier:@"newColumn"];
  
  // do binding magic somehow
  // [[newColumn dataCell] bind:NSValueBinding toObject:??? withKeyPath:??? options:nil];
  
  [self.table addColumn:newColumn]; // the table's connected with an IBOutlet
  </code></pre>
  
  <p>I've tried so many combinations of binding from/to the data cell, the table, the column, the array controller, the array itself, as well as all manner of keypaths, but nothing works. The new column is added just fine, but it's never populated.</p>
  
  <p>I imagine that the basic NSTableColumn instance I create is cell-based, not view based, and that's causing trouble. However, I have no idea how to proceed from here, and the docs never talk about binding columns that have been added programmatically.</p>
  
  <p>In this case, all I want is a simple text column, bound to the model, exactly like the one I can easily set up IB.</p>
  
  <p>I could do all this by implementing an <code>NSTableViewDataSource</code> and "manually" feeding the table, but I've got everything else set up with bindings already.</p>
  
  <p>Any help would be greatly appreciated.</p>