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:
?
NSTableCellView
subclass to the table in the appropriate places i.e. you've made sure that what you're casting to yourMyCustomView
is actually said class? – Jay