objective-ccocoamacosnsoutlineviewnscell

NSOutlineView Changing disclosure Image


I my outline view, i am adding Custom cell, To drawing custom cell, i am referring example code , present in the Cocoa documentation

http://www.martinkahr.com/2007/05/04/nscell-image-and-text-sample/

I want to change the disclosure image of the cell with my custom image, i have tried following things

- (void)outlineView:(NSOutlineView *)outlineView willDisplayCell:(id)cell forTableColumn:(NSTableColumn *)tableColumn item:(id)item 
    {
        if([item isKindOfClass:[NSValue class]])
        {
            MyData *pDt = (MyData *)[item pointerValue];
            if(pDt->isGroupElement())
            {
                [cell setImage:pGroupImage];
            }
        }
}

but that too not working, Is there any other way to change the disclosure image, also how can i find out in willDisplayCell whether Item is expand or collapse, so i can set the image accordingly,

Is this only the place to change the disclosure image ?


Solution

  • You've got the basic idea but what you will need to do is draw the image yourself. Here's the code I use:

    - (void)outlineView:(NSOutlineView *)outlineView willDisplayOutlineCell:(id)cell forTableColumn:(NSTableColumn *)tableColumn item:(id)item {
        NSString *theImageName;
        NSInteger theCellValue = [cell integerValue];
        if (theCellValue==1) {
            theImageName = @"PMOutlineCellOn";
        } else if (theCellValue==0) {
            theImageName = @"PMOutlineCellOff";
        } else {
            theImageName = @"PMOutlineCellMixed";
        }
    
        NSImage *theImage = [NSImage imageNamed: theImageName];
        NSRect theFrame = [outlineView frameOfOutlineCellAtRow:[outlineView rowForItem: item]];
        theFrame.origin.y = theFrame.origin.y +17;
        // adjust theFrame here to position your image
        [theImage compositeToPoint: theFrame.origin operation:NSCompositeSourceOver];
        [cell setImagePosition: NSNoImage];
    }
    

    You will need 3 different images as you can see, one for the ON state, one for the OFF state and also one for the MIXED state which should be halfway between the two. The mixed state makes sure you still get the opening and closing animation.