Objective c NSTableView单元格显示问题

Objective c NSTableView单元格显示问题,objective-c,nstableview,Objective C,Nstableview,我使用的是基于视图的NSTableView,遇到了一个小问题 我试图在高亮显示时将两个标签的文本颜色从黑色切换为白色 为此,我编写了以下代码 - (void)tableViewSelectionDidChange:(NSNotification *)notification { NSView * viewInQuestion = [table viewAtColumn:0 row:[table selectedRow] makeIfNecessary:YES]; if ([vi

我使用的是基于视图的NSTableView,遇到了一个小问题

我试图在高亮显示时将两个标签的文本颜色从黑色切换为白色

为此,我编写了以下代码

- (void)tableViewSelectionDidChange:(NSNotification *)notification
{
    NSView * viewInQuestion = [table viewAtColumn:0 row:[table selectedRow] makeIfNecessary:YES];

    if ([viewInQuestion isNotEqualTo:lastViewSelected])
    {
        [(NSTextField*)lastViewSelected.subviews.lastObject setTextColor:NSColor.blackColor];
        [(NSTextField*)[lastViewSelected.subviews objectAtIndex:1] setTextColor:NSColor.grayColor];
    }

    [(NSTextField*)viewInQuestion.subviews.lastObject setTextColor:NSColor.whiteColor];
    [(NSTextField*)[viewInQuestion.subviews objectAtIndex:1] setTextColor:NSColor.whiteColor];

    lastViewSelected = viewInQuestion;
}
这很有效;我得到这个结果:

问题是,有时文本不会显示为白色,即使NSLog告诉我NSTextField的颜色是NSCalibratedWhite(或其他名称)

当文本字段不可见时,颜色也会切换回黑色(滚动离开文本字段,然后返回)。然而,即使这样做,NSTextField的颜色仍然记录为白色


我的方法非常粗糙,可能不是最佳解决方案;但它解决了问题,所以这很好

假设您实现了tableSelectionDidChange,您所需要做的就是注册一个NSNotification,并实现一个更显式的自定义方法

在应用程序的init、awake或didFinishLaunching部分

NSView * contentView = table.enclosingScrollView.contentView;
[contentView setPostsFrameChangedNotifications:YES];
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(boundsDidChange:) name:NSViewBoundsDidChangeNotification object:contentView];
在程序的其他地方

(假设hasUpdatedCell是布尔属性)


然后应该正确地显示内容。

我的方法非常粗糙,可能不是最佳解决方案;但它解决了问题,所以这很好

假设您实现了tableSelectionDidChange,您所需要做的就是注册一个NSNotification,并实现一个更显式的自定义方法

在应用程序的init、awake或didFinishLaunching部分

NSView * contentView = table.enclosingScrollView.contentView;
[contentView setPostsFrameChangedNotifications:YES];
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(boundsDidChange:) name:NSViewBoundsDidChangeNotification object:contentView];
在程序的其他地方

(假设hasUpdatedCell是布尔属性)


然后事情应该会正确显示。

在NSTableViewCell上覆盖setBackgroundStyle对我来说非常有效,至少在OS X 10.8上是这样。(考虑到这里相关问题的数量,我们可以猜测,以前曾出现过一些问题。)

背景样式在选择事件和窗口激活/停用时更新,正如人们所期望的那样

这是我的自定义单元格impl-尽可能简单:

@implementation RuntimeInstanceCellView

- (void)setBackgroundStyle:(NSBackgroundStyle)backgroundStyle {
    [super setBackgroundStyle:backgroundStyle];
    self.detailTextField.textColor = (backgroundStyle == NSBackgroundStyleLight ? [NSColor darkGrayColor] : [NSColor colorWithCalibratedWhite:0.85 alpha:1.0]);
//    self.detailTextField.textColor = (backgroundStyle == NSBackgroundStyleLight ? [NSColor blackColor] : [NSColor whiteColor]);
}

@end

在NSTableViewCell上重写setBackgroundStyle对我来说非常有效,至少在OS X 10.8上是这样。(考虑到这里相关问题的数量,我们可以猜测,以前曾出现过一些问题。)

背景样式在选择事件和窗口激活/停用时更新,正如人们所期望的那样

这是我的自定义单元格impl-尽可能简单:

@implementation RuntimeInstanceCellView

- (void)setBackgroundStyle:(NSBackgroundStyle)backgroundStyle {
    [super setBackgroundStyle:backgroundStyle];
    self.detailTextField.textColor = (backgroundStyle == NSBackgroundStyleLight ? [NSColor darkGrayColor] : [NSColor colorWithCalibratedWhite:0.85 alpha:1.0]);
//    self.detailTextField.textColor = (backgroundStyle == NSBackgroundStyleLight ? [NSColor blackColor] : [NSColor whiteColor]);
}

@end

如果将答案更改为解决方案,则以我的方式处理背景样式一点也不理想。如果将答案更改为解决方案,则以我的方式处理背景样式一点也不理想。