tags:

views:

448

answers:

1

I have an outline view delegate and am overriding outlineView:dataCellForTableColumn:item: to make the cells in my outline view into buttons (see this question). Here is the code form my delegate:

- (NSCell *)outlineView:(NSOutlineView *)outlineView dataCellForTableColumn:(NSTableColumn *)tableColumn item:(id)item;
{
    MyCell * myCell = [[MyCell alloc] init];
//  return nil;
    return myCell;
}

Doing this has a strange side effect. In my outline view's data source, the method outlineView:objectValueForTableColumn:byItem: always gets a null value for tableColumn.

The code is:

- (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
{
    printf("tableColumn:%s\ttable identifier: %s\n", [[tableColumn className] cString], [[tableColumn identifier] cString]);
    return [item valueForKey:[tableColumn identifier]];
}

And the output is:

tableColumn:(null)  table identifier: (null)

What's strange is that this only happens when I implement the outlineView:dataCellForTableColumn:item: method. What am I missing here?

EDIT:

Modifying the delegate function like this seems to fix the problem:

- (NSCell *)outlineView:(NSOutlineView *)outlineView dataCellForTableColumn:(NSTableColumn *)tableColumn item:(id)item;
{
    printf("delegate column identifier: %s\n", [[tableColumn identifier] cStringUsingEncoding:NSASCIIStringEncoding]);
    if (tableColumn == nil)
    {
     return nil;
    }
    MyCell * aCustomCell = [[MyCell alloc] init];
    return aCustomCell;
}

However, I don't really understand what's going on here. If anyone can explain, that would be helpful. Thanks!

+3  A: 

NSOutlineView has the ability for you to use a single cell to draw an entire row, rather than a separate cell for each column. It first asks for a cell passing in a nil table column. If you return a cell for that call, it uses it to draw the whole row, otherwise it continues and asks for a separate cell for each column. So, as you discovered, the solution is to return a nil cell when passed a nil table column, so things will draw normally.

Brian Webster