Search code examples
objective-ccocoadatasourcensoutlineview

NSOutlineView + DataSource properly setup. How to add ImageAndTextCell?


I have properly setup an NSOutlineView with its data source and it's working great.

It's actually a basic File explorer, showing the folder structure of a particular path (folders, subfolders, etc). The subfolders are loaded on demand (when a folder item is expanded only then are its contents loaded, for speed reasons).

What I want is to know HOW I could EASILY add support to what I already have for the ImageAndTextCell class, so that I can put e.g. a folder/file icon next to each entry...

Any help is appreciated. (Please don't point me to documentation; I've studied almost all of it; what I need is advice by some who has done it, so that I just ADD to my existing code; without having to rewrite from scratch or totally change the logic...)

Thanks

Here's *My Code * (I had some trouble formatting it for SO... so I posted it on Snippet.MX)


Did what suggested and all the outline view items' names are suddenly NOT appearing...

enter image description here


Solution

  • Documentation is your friend but I understand sometime is so huge.

    BTW You need to set the ImageAndTextCell for your outline view, you can do it on you window controller init or awakeFromNib method or directly (if needed) on your NSOutlineView subclass.

    On my project I've a NSOutlineView subclass as shown below

    // myOutlineView subclass
    
    - (void)awakeFromNib {
        self.imageCell = [[ImageAndTextCell alloc] init];
        [self.imageCell setEditable: NO];
        NSTableColumn* leftColumn = [[self tableColumns] objectAtIndex:0];
        [leftColumn setDataCell: self.imageCell];
    }
    

    Then you need to implement willDisplayCell delegate method where you set the image for your specific column as shown below (folder icon, file txt icon, jpg icon).

    On the code shown below I get it from my singleton VDIconUtils but you can simply return a NSImage

    - (void)outlineView:(NSOutlineView *)outlineView willDisplayCell:(id)cell forTableColumn:(NSTableColumn *)tableColumn item:(id)item {
        [cell setDrawsBackground:NO];
        if ([[tableColumn identifier] isEqualToString:@"mycellname"]) {
            [cell setImage:[[VDIconUtils sharedIconUtils] iconForFolderStatus:fs :16 :[outlineView isItemExpanded:item]]];
        }
    
    }