Custom NSTableCellView labels not changing text color when selected
Asked Answered
H

4

17

I have a custom NSTableCellView with 3 textfields, 1 that came along and 2 others that i created myself. Here's the problem:
enter image description here

The textfields' text color stays the same even when i click on the row. I've tried to implement a code i found out by googling but it doesn't work. My Custom NSTableCellView code is:

- (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];
}

What can i do to make the labels' text color white when the user clicks on them?

Hellbox answered 20/10, 2012 at 15:2 Comment(6)
where is textfield over their, all are labels right?Hesitant
yes, that's right. changed the question to avoid misunderstandingsHellbox
just get the cell in didSelect using cellForRow and set the colors of the labels in the cell..Hesitant
create an "Answer this question" with some example code and i'll accept itHellbox
i have answered below check out..Hesitant
The colors you should be using are [NSColor textColor] and [NSColor selectedTextColor]. Your colors might be unusable with my selection color.Bludgeon
A
19

Actually, overriding setBackgroundStyle on NSTableViewCell has worked perfectly for me, at least on OS X 10.8. It is updated on selection events and on window activation/deactivation.

Here's my custom cell impl — as trivial as it can get:

@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
Agreeable answered 20/5, 2013 at 5:52 Comment(6)
Why do you call super:setBackgroundStyle ?Obaza
@DantheMan: First, because it's the right (i.e. the default) thing to do unless you have a reason not to. Second, it sets the text color of the default text field, and probably sets the background color as well.Agreeable
This doesn't work in Swift 1.2 because of this: #28719077Matelote
@SeanMoubry Can't test with Swift 1.2, but in current Swift 2.2 it works: override var backgroundStyle: NSBackgroundStyle { get { return super.backgroundStyle } set { super.backgroundStyle = newValue } }Lindi
@LarsBlumberg As another answer states, the correct thing to do in cases like this is to use didSet.Agreeable
I agree, didSet is more straight forward. Using set and get is however not incorrect, it can also be considered correct.Lindi
C
13

Expanding on the accepted answer, in Swift 2.0 the process is slightly different. Override the backgroundStyle property of your NSTableCellView subclass to add a 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()
            }
        }
    }

}
Cainozoic answered 8/10, 2015 at 9:4 Comment(1)
tanks so mutch!Proud
B
5

And for Swift 3 & 4 (isn’t this fun?):

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
        }
    }
}
Bobbie answered 8/3, 2018 at 17:5 Comment(0)
H
-5

In your tableViewSelectionDidChange get the cell using

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];

This will work for you.

You may get problem when you select other cell again after this, at that time previous cell may have still white colored labels. If this causes problems to you just have a NSIndexPath instance in your header class which represents previous selected indexPath, using this you can set back to default colors after selecting a new cell.

Hesitant answered 20/10, 2012 at 15:37 Comment(2)
btw, change the didSelectRowForIndexPath to tableViewSelectionDidChange. there's no didSelectRowForIndexPath for NSTableView.Hellbox
Funny how a answer with UIKit code has been marked as correct although the question was about OS X/AppKit...Parquetry

© 2022 - 2024 — McMap. All rights reserved.