Implementing a basic list of sources using NSOutlineView and NSTableCellView

I am new to Cocoa, but I am an experienced programmer (mainly C, C ++, .NET and QT). I am working on implementing a list of sources, which is the sidebar that you usually see in finder and iTunes. I am trying to do this using a very simple test case. So, no CoreData, no TreeController, etc.

I have implemented NSOutlineViewDataSource. Here is my code:

@implementation OutlineViewDataSource - (id)init { self = [super init]; if (self) { header = [[NSString alloc] initWithString: @"Header"]; child1 = [[NSString alloc] initWithString: @"Child 1"]; child2 = [[NSString alloc] initWithString: @"Child 2"]; child3 = [[NSString alloc] initWithString: @"Child 3"]; } return self; } - (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item { if (item == nil) { return header; } else { if (index == 0) { return child1; } if (index == 1) { return child2; } if (index == 2) { return child3; } } return nil; } - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item { if (item == nil) { return YES; } if (item == header) { return YES; } return NO; } - (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item { if (item == nil) { return 1; } if (item == header) { return 3; } return 0; } - (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item { if (item == nil) { return @""; } return item; } @end 

My xib file is basic. Its just a list of sources (which is an NSOutlineView configured to look like a list of sources. It also comes with two pre-configured NSTableCellViews. I have connected a data source and a delegate.

If I run this code with the source list configured as Cell Based, it works as expected .... but it is obvious that only text is displayed, not NSTableCellViews. If I set up the source list based on the view, I get weird behavior. NSTableCellViews are not displayed. But the extension buttons do, and I can choose the right number of elements, as if they were there. The text is not displayed. In addition, the objectValueForTableColumn function is not executed. If I add another NSTableCellView to the list of sources in xib, NSTableCellView will appear, but I cannot change the icon or text in it, objectValueForTableColumn is still not running. I tried to implement viewForTableColumn, but this function was also never called.

What am I doing wrong. I feel like I'm near, but I am missing something basic. Thank you in advance.

+4
source share
1 answer

This is a viewForTableColumn symptom that is not called. He really has to do his job, otherwise you will get empty cells. I had the same problem.

Are you sure your NSOutlineView delegate is bound to any class that implements viewForTableColumn?

-1
source

All Articles