Search code examples
objective-cbindingnstableviewnstablecellview

Select NSTableViewCell via Bindings (without tableView:viewForTableColumn:row:)


Can I use bindings to magically select the type of NSTableCellView that I want?

I've successfully transitioned from cell-based to view-based NSTableViews. Now I want to transition from using delegate methods to bindings.

Using delegate I implement tableView:viewForTableColumn:row in order to return one of three NSTableCellViews that I want to use (depending on the type of data). I set objectValue and the NSTableCellViews use that for displaying data.

Even now, with bindings, I can use the same delegate method to return one of the views; I simply don't set objectValue, and the I get objectValue from bindings. It works.

I switched to bindings as a learning experience, but also to get rid of this code. Maybe tableView:viewForTableColumn:row is specifically a delegate method instead of datasource method because this is still the expected way to select the correct view?

Is there a better (or rather, bindings-only) way to accomplish this?


Solution

  • One possible approach is to make one compound view that combines your three separate ones. You can basically embed your existing views into a tab view that doesn't show its tabs. Then, you can bind the tab view's selectedIndex or selectedIdentifier binding to a key path going through objectValue to pick which one to display.