0

I use the same NSTableCellView for both top-level and other items. I set the text color the same way for all items:

- (nullable NSView*) outlineView:(NSOutlineView *)outlineView viewForTableColumn:(nullable NSTableColumn *)tableColumn item:(id)item_;
{
    NSTableCellView *cell = [outlineView makeViewWithIdentifier:@"DataCell" owner:self];
    cell.textField.stringValue = @"hi";
    cell.textField.textColor = NSColor.redColor;
    return cell;
}

Which results in top-level items having some kinda system color - not red.

enter image description here

My remaining relevant code:

- (BOOL) outlineView:(NSOutlineView *)outlineView isGroupItem:(id)item_;
{
    return YES // only for the first item;
}

I've tried to subclass NSTableHeaderCell as advised in other topics but it's not working.

This is how I try to change the headerCells:

- (void) viewDidLoad;
{
    for (NSTableColumn *col in self.ov_sidebar.tableColumns) {
        col.headerCell = [NCTableHeaderCell_customizable new];
    }

    self.ov_sidebar.delegate = self;
    self.ov_sidebar.dataSource = self;
}

But none of the NCTableHeaderCell_customizable overiden methods are called. So I guess the custom headerCells must be set some other way. Likely it's not even the proper way to change to text color.

Do you have any clue how to properly set text color of top-level NSOutlineView items?

One possible lead, when I change the "Highlight" from "Source List" to "None" then it shows the list like this:

enter image description here

It has proper font color but unwanted formatting with disclosure and background with border.

So, shall I continue this way and try to fix this formatting as needed or is there a solution with "Source List"?

PerfectGamesOnline.com
  • 1,774
  • 1
  • 18
  • 31

1 Answers1

1

You are almost there. You May try this.

for (NSTableColumn *col in self.ov_sidebar.tableColumns) {
    col.headerCell = [[MyTableHeaderCell alloc]initTextCell:@"hi"];
}

There are always some tradeoffs even you directly subclass from NSTableHeaderCell.

  @interface MyTableHeaderCell : NSTableHeaderCell

  @end
  @implementation MyTableHeaderCell

  -(NSColor *) textColor{
   return NSColor.blueColor;
  }

 @end

This works AND is not recommended as they are not mentioned in documentation of NSTableHeaderCell.

I show a simple way to achieve your goal.

In IB add an extra TextField. and Tag it 1001 or whatever you like. your first one is textField of cellView and remember to hide it in IB. right! hide it but not remove it.

Then change code to refer to the second textfield.

NSTableCellView *cell = [outlineView makeViewWithIdentifier:@"DataCell" owner:self];
NSTextField * field = (NSTextField * )[cell viewWithTag:1001];
field.stringValue = ((MyData *) item_).value;
field.textColor = NSColor.redColor;

return cell;

enter image description here

enter image description here

E.Coms
  • 11,065
  • 2
  • 23
  • 35
  • Is it allowed to replace the header cell with a `NSTextFieldCell` instead of a `NSTableHeaderCell`? What are the side effects? How is this an answer to the question? – Willeke Sep 11 '18 at 20:47
  • col.headerCell = (NSTableHeaderCell *) [[NSTextFieldCell alloc]initTextCell:@"hi"]; ( NSTextFieldCell is the superclass of NSTableHeaderCell, so it works). The idea is to skip the default cell if you really want to change the header property. – E.Coms Sep 11 '18 at 21:28
  • You can replace a class by a subclass to change functionality, not the other way around. `NSCell` is also a superclass, will it work? Why is the default cell not a `NSTextFieldCell`? – Willeke Sep 11 '18 at 22:02
  • You are right. Since headerCell is not read-only, we can replace with any NSCell class. If you wanna keep most function, you can try to subclass directly from NSTableHeaderCell. – E.Coms Sep 11 '18 at 22:56
  • I update the answer to a NSTableHeaderCell subclass – E.Coms Sep 11 '18 at 23:41
  • Better, but still doesn't answer the question. – Willeke Sep 12 '18 at 01:04
  • This likely works for changing the header cell color but I try to change the top-level item color. Sorry for misleading you, I've tried changing headerCell as one possible solution of my problem - evidently a wrong way. Thanks anyway for your effort. – PerfectGamesOnline.com Sep 12 '18 at 08:09
  • I appreciate your help with this. This however looks like a hack. I may end up with this... ;-) I have another issue which is related with your suggestion (I've used your code there:) https://stackoverflow.com/questions/52315708/how-to-change-nstableview-header-background-color-in-mac-os-x-app – PerfectGamesOnline.com Sep 13 '18 at 14:20
  • I wanted to use the SourceList type so this hacky way is after all the only working way. Thanks man. – PerfectGamesOnline.com Sep 15 '18 at 16:40
  • @E.Coms - thanks this solved a longstanding problem for me. BTW you can also just use IB to bind the second text field to an IBOutlet in a subclassed NSTableCellView. – Duncan Groenewald Dec 13 '19 at 04:19