1

When I attempt to construct a view per row for my view-based NSTableView the method makeViewWithIdentifier:owner: always returns nil. The table is build in IB and all seems to be linked properly, the delegate methods are all called as I'd expect. When looking through the Apple documentation about the makeViewWithIdentifier -method I can't find a reason why the method will return nil. A snippet from the code:

    - (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
    if ([tableColumn.identifier isEqualToString:@"foo"]) {
        NSTableCellView *view = [tableView makeViewWithIdentifier:@"id" owner:self]; // Is always nil..
        view.textField.stringValue = [myArray objectAtIndex: row];

        return view;
    }
    return nil;
}

Why could view be nil?

shallowThought
  • 19,212
  • 9
  • 65
  • 112
  • I suspect @"id" does not fit the views ID. – shallowThought Jan 14 '17 at 17:50
  • What is the reason not to use the same identifier as the column identifier? – vadian Jan 14 '17 at 17:50
  • @vadian You just saved me, but I really do not understand why. Changing the view identifier arg to the same as the column fixed it. I thought the view identifier was only used for looking up a certain view or something. Well, learned something new at least! – Mark Hasselbach Jan 16 '17 at 22:50

1 Answers1

0

You must use the same identifier in xib and code:

static NSString * const ItemMainTableCellViewIdentifier = @"ItemMainTableCellViewIdentifier";

nib = [[NSNib alloc] initWithNibNamed:@"ItemMainTableCellView" bundle:nil];
[self.table registerNib:nib forIdentifier:ItemMainTableCellViewIdentifier];

ItemMainTableCellView *cellView = [tableView makeViewWithIdentifier:ItemMainTableCellViewIdentifier owner:self];

enter image description here

93sauu
  • 3,770
  • 3
  • 27
  • 43