NSTableView (and HTML tables)

I remember when I first wrote a web page that had a table in it which contained more than just text. It didn’t work on the majority of our university’s web browsers (Mosaic was the most commonly used, although Netscape 2.0 could show the images just fine). Very soon thereafter, tables were being used for … Continue reading “NSTableView (and HTML tables)”

I remember when I first wrote a web page that had a table in it which contained more than just text. It didn’t work on the majority of our university’s web browsers (Mosaic was the most commonly used, although Netscape 2.0 could show the images just fine). Very soon thereafter, tables were being used for all sorts of stuff (and then became overused for layout, but that’s not what this post is about).

The good thing about an HTML table is that I can specify how many rows and columns I want, and what I want in each individual cell created by those rows and columns.

With my recent forays into Cocoa development, I’ve been trying to use NSTableView. NSTableView, on the surface, appears to be much like the original intent behind HTML tables. Make a table with X columns and Y rows and populate it thusly. And much like Mosaic, this will work fine and dandy when you populate with text.

I’ve been trying to add non-text objects in the cells, and having a lot of difficulty. Chief among my difficulties is understanding the paradigm that Apple has for its tables. You specify the number of columns as part of the NSTableView object, then tell the NSTableView what its data source is (another object which has some specific methods in it). That data source tells the NSTableView how many rows there are (which makes sense when you think about it – how will the interface know in advance how many records to display?).

However, what doesn’t seem to work well for me is in saying “Put object Z in column X/row Y” where Z is a non-text cell (the cases I’ve been trying are NSSegmentedCell and NSPopUpButtonCell). I look at example code, and no-one else seems to be programming this way, so I presume I’m going against the paradigm. Everyone else seems to return just pure data from their data source, and their view/controller code somehow knows how to transform that into the correct representation in the NSTable.

Part of this issue might come from the overlap between Interface Builder and my code. In IB, I can specify what type of data cell each column should have (I can do the same in XCode), and I can link my NSPopUpButtonCell to a menu that I’ve created in IB.

But I also think that part of the issue comes from a non-intuitive API for the NSTableView. It could be that I’m just used to HTML tables, and “real” GUI developers will be tutting over their lattés as they read this (if we have any real GUI dev readers). Still, I know I’ve never had this problem in other GUI paradigms either (e.g. Tk or Swing – although I don’t care to switch to either of those ;-)).

Leave a Reply