cocoanstableviewnstablecolumnnstableheaderviewnstableheadercell

How to set the column name of an NSTableColumn in a view-based NSTableView?


I've created an NSTableView programmatically and added a single column (an instance of NSTableColumn). How can I set the name of this column (by default it's named "Field")? In NSTableColumn there's a method with signature

- (void)setHeaderCell:(NSCell *)aCell

but "[t]his method is only valid for cell-based table views", and I am using a view-based table view.

The NSTableView itself contains an instance of NSTableHeaderView, which contains an instance of NSTableHeaderCell. NSTableHeaderCell is a subclass of NSTextFieldCell, which looks good, since it has a setString method. But I can't access the NSTableHeaderCell! So how then can I set the column name? I assume it's trivial to do it: after all, being able to set the column names in a table view is pretty much the most important thing to be able to do with a table view, but after hours of searching I haven't found a single clue on how to do it.


Solution

  • I think you should just go ahead and use -[NSTableColumn headerCell]. The comment in the class reference is not present in the NSTableColumn.h header file, even though those table-view-related headers have pretty thorough documentation about view-based table views. Also, although Apple has generally deprecated NSCell in 10.10 and -[NSTableColumn dataCell] is deprecated, -headerCell is not. That suggests to me that Apple doesn't have a replacement.