Search code examples
interface-buildernstableviewnsoutlineviewnstablecellview

Custom view in NSTableView / NSOutlineView


I have a view-based NSOutlineView that I am providing a custom view (a subclass of NSTableCellView) for one of the columns. I am registering this subview using:

let nib = NSNib(nibNamed: "MyMatcherView", bundle: NSBundle.mainBundle())
outlineView.registerNib(nib, forIdentifier: "matcher")

When I call outline.makeViewWithIdentifier("matcher"), it successfully returns me an instance of my custom view. Yay!

In Interface Builder, though, I've noticed that I can totally delete the NSTableCellView control from the matcher column (see below).

Table view in Interface Builder

Two questions arise:

  • I assume that it works at runtime because the call to registerNib() tells the table to use that nib, instead of the one that would have been defined by Interface Builder. Is this correct?
  • In the context of me providing a custom view, what is the Text Cell control that you can see in the screenshot below. I can't delete it, and I am not really sure what it is representing.

Solution

  • The views you can define in IB within a table column are actually in sub-NIBs (NIBs-within-a-NIB) and they are pre-registered based on their identifiers. So, yes, when you register a new NIB for a given identifier, you replace the one from the NIB containing the table view. It won't be used anymore so you can delete it from that NIB.

    The Text Cell is a vestige from cell-based table views. It can't be removed in IB and even if you remove it by text-editing the .xib file, it will be restored the next time you open it in IB. I'm not sure why IB insists on keeping it and doesn't just hide it. You should just ignore it (although you should probably clear its title in the Attributes inspector if you export strings for localization, or your translators will be asked to translate "Text Cell" for no good reason).