Search code examples
cocoacustom-controlsnsoutlineview

NSView based custom control drawing update is delayed. Why?


I have a view based NSOutlineView which displays entries (Source entity) from a Core Data store. The view in the outline view uses a custom control which is implemented as a subclass of NSView. This control displays a round coloured marker based on a numerical value (0-7). This value is stored as an attribute of the Source entity and is intended as a method to implement a Finder-like method of labeling.

The whole thing is wired using bindings with IB.

I have attached a screenshot which will hopefully make my intentions clear.

It all works good but for one really annoying detail. When the numerical value is changed (from the right side of the screen) the custom control is only updated when the selection in the outline view is changed. Obviously it would be nicer to have this change reflected immediately but I've failed so far. I've tried various scenario's with setNeedsDisplay: YES which were all basically ignored.

Any ideas?

enter image description here

Edit: I implemented a setter with the custom control:

- (void) setLabelValue: (NSNumber*) aValue {
    labelValue = aValue;
    [self setNeedsDisplay: YES];
}

Reasoning that the setNeedsDisplay: would trigger a re-draw, in the drawRect: method I query the value to establish the proper color:

- (void)drawRect: (NSRect) dirtyRect {
    // Label value between '1' and '7' indicate that a label was assigned. Determine label color and border color.
    if ([[self labelValue] intValue] > 0) {
        NSColor *aBackgroundColor = [NSColor clearColor];
        switch ([[self labelValue] intValue]) {
            case 1:
                aBackgroundColor = [NSColor colorWithCalibratedRed:...];
                break;
            case 2:
                aBackgroundColor = [NSColor colorWithCalibratedRed:...];        
                break;
            case 3:
                aBackgroundColor = [NSColor colorWithCalibratedRed:...];
                break;
            case 4:
                aBackgroundColor = [NSColor colorWithCalibratedRed:...];    
                break;
            case 5:
                aBackgroundColor = [NSColor colorWithCalibratedRed:...];        
                break;
            case 6:
                aBackgroundColor = [NSColor colorWithCalibratedRed:...];            
                break;
            case 7:
                aBackgroundColor = [NSColor colorWithCalibratedRed:...];        
                break;
        }
        // Draw border first.
        ...
        // Draw label color.
        ...
    } 
    // Label value of '0' indicates that no label was assigned.
    if ([[self labelValue] intValue] == 0) {
        NSBezierPath *aPath = [NSBezierPath bezierPathWithRoundedRect: ...];
        [[NSColor clearColor] set];
        [aPath fill];
    }
}

Solution

  • I reimplemented the whole thing using notifications. I could not get it to work using bindings. Here's what I did:

    The controller of the NSOutlineView (left part of the screen) distributes views for its elements using the method:

    - (NSView *) outlineView: (NSOutlineView *) outlineView viewForTableColumn: (NSTableColumn *) tableColumn item: (id) anItem
    

    When an entity associated to a file (an image, PDF etc) is added to the outline view, the item view (custom subclass of NSTableCellView) for that particular item registers an interest in changes to the labelValue property of the entity:

    [[NSNotificationCenter defaultCenter] addObserver: aView selector: @selector(labelValueChanged:) name: @"LabelValueChanged" object: nil];
    

    When a change in the labelValue property occurs (through a click on one of the buttons in the right part of the screen), that controller fires a notification:

    [[NSNotificationCenter defaultCenter] postNotificationName:@"LabelValueChanged" object:[self selectedSource]];
    

    Throught the Notification system, the method labelValueChanged: gets called by the item view (in the NSOutlineView) which invalidates the display of the custom view components that actually draws the label:

    - (void) labelValueChanged: (NSNotification*) aNotification {
        // A notification was received that the label was changed. Mark the label object of the receiver
        // if the object in the notification (anObject) matches the object of the receiver (objectValue).
        id anObject = [aNotification object];
        if (anObject == [self objectValue]) {
            [[self label] setNeedsDisplay:YES];
        }
    }
    

    Thanks to @KenThomases for his suggestions.