4

The log lines below will show "Table cell: [valid value logged here]" but "Text field: (null)".

I'm actually trying to build an NSOutlineView programmatically, and it works with a cell-based approach, but not with a view-based approach. Can someone tell me what I'm doing wrong?

-(NSView*)outlineView:(NSOutlineView *)outlineView viewForTableColumn:(NSTableColumn *)tableColumn item:(id)item {
    NSTableCellView* view = [outlineView makeViewWithIdentifier:tableColumn.identifier owner:self];
    if (view == nil) {
        view = [[NSTableCellView alloc] init];
        view.identifier = tableColumn.identifier;
    }

    view.textField.stringValue = @"TEST";

    NSLog(@"Table cell: %@", view);
    NSLog(@"Text field: %@", view.textField);

    return view;
}
magnus
  • 4,031
  • 7
  • 26
  • 48
  • You didn't create a textField instance there!!! – Anoop Vaidya Nov 24 '14 at 10:25
  • I was under the impression that the text field already existed? And also, when I tried `view.textField = [[NSTextField alloc] init]`, the application raised a EXC_BAD_ACCESS. – magnus Nov 24 '14 at 10:29
  • I believe you should create a seperate textField object and `addSubview:` it to the outline's view . – Anoop Vaidya Nov 24 '14 at 10:32
  • ... or override `NSTableCellView` and let the subclass do it. Which is a superior approach. – trojanfoe Nov 24 '14 at 10:41
  • Subclassing is always a better option in these requirements. – Anoop Vaidya Nov 24 '14 at 10:48
  • 2
    Why is that better? Shouldn't NSTableCellView work "out of the box"? Creating a source list programmatically is rediculously difficult. – magnus Nov 24 '14 at 20:09
  • If you're going to use ``[[NSTextField alloc] init]`` to set the ``textField`` property, you need to create it, then add it as a subview of your ``NSTableCellView`` (``[view addSubview:tf]``). The ``EXC_BAD_ACCESS`` error might be caused by omitting this step. You'll also need to set the frame of your field in this method - or better, constrain it. – Paul Patterson Nov 24 '14 at 22:36

0 Answers0