0
votes

I have a view-based NSTableView with my custom view and the normal NSTextField in it. The custom view's background colour should change when a user clicks and it doesn't work because drawRect is not being called. I don't understand why really. Here's the delegate method and custom view code.

MyController

- (void)didAddLabel:(id)sender {
    CustomLabel *label = (CustomLabel *)sender;

    NSTableRowView *aaa = [_tableView rowViewAtRow:_tableView.selectedRow makeIfNecessary:NO];
    MyCustomView *hello = [((NSTableCellView*)[aaa viewAtColumn:0]).subviews objectAtIndex:0];

    [hello setBackgroundColor:[label nscolor]];
    NSLog(@"Background colour is %@", [hello backgroundColor]);
    [hello setNeedsDisplay:YES];
}

My view

class MyCustomView: NSView {

    var backgroundColor :NSColor  = NSColor.purpleColor()

    override func drawRect(dirtyRect: NSRect) {
        NSLog("In here")
        backgroundColor.setFill()
        NSRectFill(dirtyRect)
    }
}

Observations: 1) the drawRect is only called upon init of the app. 2) The NSLog row contains the new/selected colour however the background does not change from purple.

Question: why isn't drawRect: called after setNeedsDisplay:?

1
.and you're definitely returning your custom NSTableCellView subclass to the table in the appropriate places i.e. you've made sure that what you're casting to your MyCustomView is actually said class?Jay
Thanks for that insight. The view actually had index 1 instead of 0. You may post your answer.H.Rabiee

1 Answers

0
votes

drawRect: is only called if you don't have layer backing turned on. Are any of the parent views / this view have wantsLayer = YES or have a CALayer assigned to it? In that case, you'll have to use updateLayer: since all children of a layer-backed view are automatically layer-backed.