9

I have a similar question to Cocoa - View-Based NSTableView, using one cell in multiple tables, amplified by Apple's own docs for makeViewWithIdentifier:owner:

"Typically identifier is associated with an external NIB in Interface Builder and the table view will automatically instantiate the NIB with the provided owner."

This seems to imply that you should be able to store the NSTableCellView in a separate nib from the nib containing the NSTableView. However, in my experimenting, I have only ever been able to obtain cells which are contained within the tableview I'm calling this on. I.e., if I cut and paste my cell into a new .xib file, the tableview can no longer find it. What am I doing wrong, or is this actually impossible and I am somehow misreading Apple's docs?

Community
  • 1
  • 1
justin k.
  • 504
  • 1
  • 6
  • 15

2 Answers2

6

Use - (void)registerNib:(NSNib *)nib forIdentifier:(NSString *)identifier to register a nib to be used with a cell identifier.

If it doesn't work you're probably registering the nib after the tableView data has been loaded. Use [tableView reloadData] afterwords to be sure it's not a timing issue.

Arlen Anderson
  • 2,486
  • 2
  • 25
  • 36
  • 1
    Hm... looks like a new method in 10.8. Are developers targeting 10.7 out of luck? – justin k. Nov 15 '12 at 16:34
  • 1
    On 10.7 you should be able to construct NSTableCellView's as top level objects in the same nib as your NSTableView. Watch out, though. Through my testing it appears the nib is instantiated every time a cell is created from it. In other words, if you have quite a few other objects in your nib, they'll all get instantiated as well. – Arlen Anderson Nov 16 '12 at 05:34
  • It's been a while since I posed the question, but IIRC the difficulty was obtaining NSTableCellView from an external nib. – justin k. Nov 16 '12 at 22:03
3

I just ran into this problem and I think you cannot use makeViewWithIdentifier:owner: when you're using a dedicated Nib to populate View-Based Tables.

The problem has to do with file owners (ie. view controllers). makeViewWithIdentifier:owner: seems intended to be used with "self" as the owner for simple custom views.

Generally if you have a separate nib for the custom view with outlets, you're going to want a separate view controller too. Otherwise, if your custom view has an outlet and the table displays many custom views, which outlet are you referring to from the "self" table view owner?

So in my test, I've got the AppDelegate as the delegate/datasource of the Table View. I have a CellView.xib, and CellViewController.h/.m with outlets to the interface. Then in my tableView:viewForTableColumn:row: delegate method I have this code:

SSCellViewController *vc = [[SSCellViewController alloc] initWithNibName:@"CellView" bundle:nil];
return vc.view;

What you lose is the cell re-use that happens automatically with makeViewWithIdentifier:owner:. To implement that yourself, you'll also likely have to deal with managing the many view controllers you've created.

I might still be missing something, as I'm coming to OS X development after years of only doing iOS work.

Silromen
  • 1,131
  • 2
  • 10
  • 18
  • 2
    You instantiate a view controller but only return its view. Who owns the view controller? Doesn't it get automatically deallocated under ARC? Doesn't it leak otherwise? – Nicolas Miari Jan 31 '15 at 13:22