Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 937
Amitg2k12 Avatar asked Dec 21 '22 17:12

Amitg2k12


2 Answers

A nice way to change the disclosure image is to use a view based outline view:

In your ViewController with NSOutlineViewDelegate:

- (NSView *)outlineView:(NSOutlineView *)outlineView viewForTableColumn:(NSTableColumn *)tableColumn item:(id)item
{
    CustomNSTableCellView *cell    = [outlineView makeViewWithIdentifier:tableColumn.identifier owner:self];
    cell.item                      = item;

    return cell;
}

You have to subclass your NSOutlineView and overide the method:

- (id)makeViewWithIdentifier:(NSString *)identifier owner:(id)owner
{
    id view = [super makeViewWithIdentifier:identifier owner:owner];

    if ([identifier isEqualToString:NSOutlineViewDisclosureButtonKey])
    {
        // Do your customization
        // return disclosure button view

        [view setImage:[NSImage imageNamed:@"Disclosure_Categories_Plus"]];
        [view setAlternateImage:[NSImage imageNamed:@"Disclosure_Categories_Minus"]];
        [view setBordered:NO];
        [view setTitle:@""];

        return view;
    }

    return view;
}

//Frame of the disclosure view
- (NSRect)frameOfOutlineCellAtRow:(NSInteger)row
{
    NSRect frame = NSMakeRect(4, (row * 22), 19, 19);
    return frame;
}
like image 37
Rikco Avatar answered Jan 06 '23 06:01

Rikco


After researching this issue myself, and trying some of the answers here, I have found that the other approaches mentioned will work, but will require that you perform a lot more manual intervention in order to avoid screen artifacts and other strange behavior.

The simplest solution I found is the following, which should work in most cases.

This solution has the added benefit of the system automatically handling a great many other cases, such as column movement, etc, without your involvement.

- (void)outlineView:(NSOutlineView *)outlineView willDisplayOutlineCell:(id)cell
     forTableColumn:(NSTableColumn *)tableColumn
               item:(id)item
{
    [cell setImage:[NSImage imageNamed: @"Navigation right 16x16 vWhite_tx"]];
    [cell setAlternateImage:[NSImage imageNamed: @"Navigation down 16x16 vWhite_tx"]];
}

In your case, you would wrap this up with your class detection logic, and set the cell images appropriately for your cases.

like image 89
Scott Allen Avatar answered Jan 06 '23 05:01

Scott Allen