【问题标题】:Custom NSTableCellView labels not changing text color when selected选择时自定义 NSTableCellView 标签不改变文本颜色
【发布时间】:2012-10-10 23:31:18
【问题描述】:

我有一个自定义的NSTableCellView,带有 3 个文本字段,其中 1 个是附带的,另外 2 个是我自己创建的。这就是问题所在:

即使我单击该行,文本字段的文本颜色也保持不变。我试图实现通过谷歌搜索找到的代码,但它不起作用。我的自定义 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];
}

当用户点击标签时,如何使标签的文本颜色变为白色?

【问题讨论】:

  • 文本字段在哪里,都是标签对吗?
  • 是的,没错。改题避免误会
  • 只需使用cellForRow 获取didSelect 中的单元格并设置单元格中标签的颜色..
  • 用一些示例代码创建一个“回答这个问题”,我会接受它
  • 我已经在下面回答了结帐..

标签: objective-c macos cocoa nstableview nstableviewcell


【解决方案1】:

在您的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,使用它您可以在选择新单元格后设置回默认颜色。

【讨论】:

  • 顺便说一句,将didSelectRowForIndexPath 更改为tableViewSelectionDidChange。 NSTableView 没有didSelectRowForIndexPath
  • 有趣的是 UIKit 代码的答案如何被标记为正确,尽管问题是关于 OS X/AppKit...
【解决方案2】:

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

【讨论】:

  • 你为什么打电话给super:setBackgroundStyle
  • @DantheMan:首先,因为这是正确(即默认)的事情,除非你有理由不这样做。其次,它设置默认文本字段的文本颜色,并且可能还设置背景颜色。
  • 这在 Swift 1.2 中不起作用,因为:stackoverflow.com/questions/28718577/…
  • @SeanMoubry 无法使用 Swift 1.2 进行测试,但在当前的 Swift 2.2 中它可以工作:override var backgroundStyle: NSBackgroundStyle { get { return super.backgroundStyle } set { super.backgroundStyle = newValue } }
  • @LarsBlumberg 正如另一个答案所述,在这种情况下正确的做法是使用didSet
【解决方案3】:

扩展已接受的答案,在 Swift 2.0 中,过程略有不同。覆盖 backgroundStyle 子类的 backgroundStyle 属性以添加 didSet property observer

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()
            }
        }
    }

}

【讨论】:

  • 坦克太笨了!
【解决方案4】:

对于 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
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-30
    • 2023-03-26
    • 1970-01-01
    • 2021-09-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多