选中时,自定义NSTableCellView标签不会更改文本颜色

时间:2012-10-20 15:02:25

标签: objective-c macos cocoa nstableview nstableviewcell

我有一个自定义NSTableCellView,包含3个文本字段,1个出现,另外2个是我自己创建的。这是问题所在:
enter image description here

即使单击该行,文本字段的文本颜色也保持不变。我试图实现我通过谷歌搜索发现的代码,但它不起作用。我的自定义NSTableCellView代码是:

- (void)drawRect:(NSRect)dirtyRect{
    NSColor *color = [NSColor colorWithCalibratedRed:(26/255.0) green:(26/255.0) blue:(26/255.0) alpha:1.0];
    [self.textField setTextColor:color];

    color = [NSColor colorWithCalibratedRed:(102/255.0) green:(102/255.0) blue:(102/255.0) alpha:1.0];
    [_lbl1 setTextColor:color];
    [_lbl2 setTextColor:color];
}

- (void)setBackgroundStyle:(NSBackgroundStyle)backgroundStyle {
    NSColor *color = (backgroundStyle == NSBackgroundStyleDark) ? [NSColor windowBackgroundColor] : [NSColor controlShadowColor];
    self.textField.textColor = color;
    self.lbl1.textColor = color;
    self.lbl2.textColor = color;
    [super setBackgroundStyle:backgroundStyle];
}

如果用户点击标签的文字颜色为白色,我该怎么办?

4 个答案:

答案 0 :(得分:17)

实际上,在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

答案 1 :(得分:9)

扩展已接受的答案,在 Swift 2.0 中,这个过程略有不同。覆盖backgroundStyle子类的NSTableCellView属性以添加didSet http://www.codeproject.com/Articles/27432/Artificial-Inheritance-Contexts-in-WPF

class CustomTableCellView: NSTableCellView {

    @IBOutlet weak var detailTextField: NSTextField!

    override var backgroundStyle: NSBackgroundStyle {
        didSet {
            if self.backgroundStyle == .Light {
                self.detailTextField.textColor = NSColor.controlTextColor()
            } else if self.backgroundStyle == .Dark {
                self.detailTextField.textColor = NSColor.alternateSelectedControlTextColor()
            }
        }
    }

}

答案 2 :(得分:0)

对于Swift 3& 4(这不是很有趣吗?):

override var backgroundStyle: NSView.BackgroundStyle {
    didSet {
        if self.backgroundStyle == .light {
            self.detailTextField.textColor = NSColor.controlTextColor
        } else if self.backgroundStyle == .dark {
            self.detailTextField.textColor = NSColor.alternateSelectedControlTextColor
        }
    }
}

答案 3 :(得分:-5)

tableViewSelectionDidChange使用

获取单元格
UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath]; //replace UITableViewCell with your customCell class name if it other
//now as u got the instance of your cell u can modify the labels in it, like
cell.lable1.textColor = [UIColor whiteColor];

这对你有用。

在此之后再次选择其他单元格时可能会出现问题,此时前一单元格可能仍有白色标签。如果这会导致问题,那么只需在头类中有一个NSIndexPath实例代表之前选择的indexPath,使用此实例可以在选择新单元格后重新设置为默认颜色。

相关问题