2012-10-20 80 views
14

我有一个自定义NSTableCellView,包含3个文本框,1个来过,另外2个是我自己创建的。这里的问题:
enter image description here自定义NSTableCellView标签在选择时不会更改文本颜色

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

我能做些什么,使标签的文本颜色为白色,当用户点击他们?

+0

哪里是文本框在他们的,都是标签对吗? – vishy

+0

是的,没错。改变了这个问题,以避免误解 –

+0

只需使用'cellForRow'在'didSelect'中获取单元格并设置单元格中标签的颜色。 – vishy

回答

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 
+0

为什么你叫'super: setBackgroundStyle'? –

+3

@DantheMan:首先,除非你有理由不这样做,否则这是正确的(即默认)做法。其次,它设置默认文本字段的文本颜色,并可能设置背景颜色。 –

+0

这不适用于Swift 1.2因为这个:http://stackoverflow.com/questions/28718577/issue-with-conforming-to-objective-c-protocol-from-swift-nsobject-subclass –

-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实例,使用此实例可以在选择新的单元格后重新设置为默认颜色。

+0

btw,将'didSelectRowForIndexPath'改为'tableViewSelectionDidChange'。 NSTableView没有'didSelectRowForIndexPath'。 –

+0

好的,谢谢..我没有开始开发Mac应用程序,所以.. – vishy

+2

有趣的是,UIKit代码的答案已被标记为正确,但问题是关于OS X/AppKit ... –

9

拓展上公认的答案,在雨燕2.0过程略有不同。重写你的NSTableCellView子类的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() 
      } 
     } 
    } 

} 
+0

坦克如此莫测! – Mike97

0

而对于斯威夫特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 
     } 
    } 
} 
相关问题