【发布时间】:2016-05-02 08:15:41
【问题描述】:
我有一个基于视图的 NSTableView,其中包含我的自定义视图和普通的 NSTextField。当用户单击时,自定义视图的背景颜色应该会发生变化,并且它不起作用,因为没有调用 drawRect。我真的不明白为什么。这是委托方法和自定义视图代码。
我的控制器
- (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];
}
我的看法
class MyCustomView: NSView {
var backgroundColor :NSColor = NSColor.purpleColor()
override func drawRect(dirtyRect: NSRect) {
NSLog("In here")
backgroundColor.setFill()
NSRectFill(dirtyRect)
}
}
观察:1) drawRect 仅在应用程序的初始化时调用。 2) NSLog 行包含新的/选定的颜色,但背景不会从紫色改变。
问题:为什么drawRect: 不在setNeedsDisplay: 之后调用?
【问题讨论】:
-
。你肯定会在适当的地方将你的自定义
NSTableCellView子类返回到表中,即你已经确保你正在投射到你的MyCustomView是 真的是上课吗? -
感谢您的洞察力。该视图实际上有索引 1 而不是 0。您可以发布您的答案。
标签: cocoa nstableview nsview