5
votes

I am new to Cocoa, but am an experienced programmer (mostly C, C++, .NET and QT). I am working on implementing a Source List which is the sidebar you typically see in finder and iTunes. I am attempting to do so using a very basic test case. So no CoreData, no TreeController, etc...

I implemented the 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 Source List (Which is an NSOutlineView configured to look like a Source list. It also comes with two NSTableCellViews pre-configured. I connected the Data Source and Delegate.

If I run this code with the Source List configured as Cell Based it works as expected.... but obvious only text is shown and not the NSTableCellViews. If I configure the Source List to be view based, I get strange behavior. The NSTableCellViews do not show up. But the expand buttons do, and I can select the correct number of items as if they are there. No text shows up either. Furthermore, the objectValueForTableColumn function doesn't execute. If I add another NSTableCellView to the Source List in the xib, the NSTableCellView shows up, but I cannot change the icon or text in it, objectValueForTableColumn still doesn't execute. I attempted to implement viewForTableColumn, but that function also in never called.

What am I doing wrong. I feel like I am close, but I am missing something basic. Thanks in advance.

1
Were you able to solve your problem? I have ran into the same problem. - user945771

1 Answers

-1
votes

This is a symptom of viewForTableColumn not getting called. It really needs to do its job, otherwise you get blank cells. I had the exact same problem.

Are you sure that your NSOutlineView's delegate is bound to whatever class implements viewForTableColumn?