Search code examples
objective-cxcodecocoanstableviewnsoutlineview

Notification when content changes in NSOutlineView subclass


I'd like to subclass NSOutlineView to show a label in the middle of itself when there is no content yet. Much like the inspectors in XCode:

enter image description here

Obviously I can't use the delegate methods, because I'm implementing this as a subclass and I have to be able to set the delegate to something else when using this class.

I didn't find any notifications that I could observe, except changes in the bounds property, but that's not very reliable.


Solution

  • I ended up overriding several methods of NSOutlineView to inject my code. It's not a very elegant solution, but it works. If anyone has a better solution, let me know and I might accept your answer instead.

    - (void)updateEmptyLabelVisibility {
        int r = [[self dataSource] outlineView:self numberOfChildrenOfItem:nil];
        BOOL hasRows = r > 0;
        _emptyLabel.hidden = hasRows;
    }
    
    - (void)reloadItem:(id)item {
        [super reloadItem:item];
        [self updateEmptyLabelVisibility];
    }
    
    - (void)reloadData {
        [super reloadData];
        [self updateEmptyLabelVisibility];
    }
    
    - (void)awakeFromNib {
        [super awakeFromNib];
        [self updateEmptyLabelVisibility];
    }
    
    - (void)endUpdates {
        [super endUpdates];
        [self updateEmptyLabelVisibility];
    }