【问题标题】:drawRect not being called for NSTableView cell view没有为 NSTableView 单元格视图调用 drawRect
【发布时间】: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


【解决方案1】:

drawRect: 仅在您未打开图层支持时调用。是否有任何父视图/此视图具有wantsLayer = YES 或分配有 CALayer?在这种情况下,您必须使用updateLayer:,因为支持图层的视图的所有子视图都会自动支持图层。

【讨论】:

  • 请看我上面的评论。
猜你喜欢
  • 1970-01-01
  • 2023-04-01
  • 1970-01-01
  • 2013-03-15
  • 2017-11-13
  • 2017-10-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多