0

I've looked at a number of questions but can't find a good solution for a View-Based NSOutlineView

Coloring NSTableView Text per row

Change color of NSTableViewCell

Custom background colors for NSTableCellView

I'm trying to set each row to whatever color I want. I've read somewhere that I need to subclass NSTableRowView which I've now done.

According to the AppleDocs, I see the following methods:

– drawBackgroundInRect:
– drawDraggingDestinationFeedbackInRect:
– drawSelectionInRect:
– drawSeparatorInRect:

How would I go about setting the background color for the individual rows? Am I going the wrong route above?

Edit: below (also edited title)

Since i'm using an NSOutlineView and not a NSTableView, when i change the background color of the cells the image looks like the following. The disclosure arrows to the left is not colored. Is there any way to change the color of the whole row for the NSOutlineView?

this

Community
  • 1
  • 1
Just a coder
  • 15,480
  • 16
  • 85
  • 138

3 Answers3

1

You could subclass NSTableViewCell, and add a method to it which sets its color.

NSTableViewCell is already a subclass of NSView, so in your subclass, you would add the following method:

- (void)setBackgroundColor {
    self.layer.backgroundColor = CGColorCreateGenericRGB(0, 0, 0, 1.0f); // or whatever color
}

Or something like that. You'll probably want to have the color be a param to the method. Then, in your table view delegate, you can set the color depending on the row index passed to the delegate method. For example:

- (UITableViewCell *)tableView:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [table dequeueReusableCellWithIdentifier:@"Cell"];
    if (cell == nil)
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];

    if (indexPath.row % 2) {
        [cell setBackgroundColor:[UIColor redColor]]; // or something like that
    }
}
svth
  • 1,303
  • 1
  • 14
  • 23
1

Came up with a solution. Implemented the following.

-(void)outlineView:(NSOutlineView *)outlineView didAddRowView:(NSTableRowView *)rowView forRow:(NSInteger)row {
    [self updateRowViewBackColorforItem:[outlineView itemAtRow:row]];
}

-(void)updateRowViewBackColorforStep:(myCustomItem *)customItem {
    static NSColor *color1;
    static NSColor *color2;
    static NSColor *color3;

    if (color1 == nil) {
        sharedcolorHeader = [NSColor colorWithCalibratedRed:(r/255.0f) green:(g/255.0f) blue:(b/255.0f) alpha:1.0];
    }
    if (color2 == nil) {
        sharedcolorChildren = [NSColor colorWithCalibratedRed:(x/255.0f) green:(y/255.0f) blue:(z/255.0f) alpha:1.0];
    }
    if (color3 == nil) {
        normalColor = [NSColor colorWithCalibratedRed:(255/255.0f) green:(255/255.0f) blue:(255/255.0f) alpha:1.0];
    }

    NSInteger row = [stepOutlineView rowForItem:step];
    if (row < 0) return;

    NSTableRowView *view = [myOutlineView rowViewAtRow:row makeIfNecessary:NO];

   if ([customItem type] == 1) {
        [view setBackgroundColor:sharedcolorHeader];
    } else if([customItem type] == 2) {
        [view setBackgroundColor:sharedcolorChildren];
    } else {
        [view setBackgroundColor:normalColor];
    }
}
Just a coder
  • 15,480
  • 16
  • 85
  • 138
  • What is this: (x/255.0f) ? A shortcut for defining floats? – ThorstenC May 29 '14 at 13:32
  • I dont know the reason why its this way, but this is the way it is. With a quick google search, i can find out why, but i dont have the time. The color value x must be divided by 255 for it to be displayed with the color you expect. – Just a coder Jun 03 '14 at 15:13
  • 2
    Ah, I see: Color values are floats between 0..1 So 1 means "Full Color" and 0 means no color. Years ago I coded on simple 8Bit VGA screens, so 255 was full 8 bit, so you have to map your 8-Bit values to a range from 0..1. Was just confused to see this in this days. – ThorstenC Jun 04 '14 at 07:23
0

This is really something that should rely on properties or ivars in your data model. If you use view based outline views, you can simply have custom views for your row views and or cell views. Have the custom views draw whatever you want based in the data in your represented object.

uchuugaka
  • 12,679
  • 6
  • 37
  • 55
  • I already have custom views. And this is where i'm stuck. How do i use my custom views to draw it? If i have sample code to draw just one color, i will know how to do the rest. – Just a coder Aug 14 '13 at 05:03
  • So if you representedObject has some way to identify the right style, preferably a property fore coding simplicity, your view cell can ask the representedObject about property XYZ. If X, do XStyling. You can do bindings in code if its something IB won't let you. The styling part is the same as any conditional logic, but likely focused on determining what to do in drawRect. Just add the properties you need in your view class. Say an NSInteger flag to set and use to determine how to draw. – uchuugaka Aug 14 '13 at 05:15
  • I dont think you are understanding what i am asking.. I know all this. I have the conditional logic (XYZ) to do the logic.. i have every thing setup. But my problem is --> How do i draw? What is the draw code? Thats what im asking all along.. i dont know the draw code.. – Just a coder Aug 14 '13 at 07:22
  • Aha. So that's it then. What do you want to draw exactly? In drawRect for a background of a containing view it's pretty simple but not easy to guess at. I can give you some pointers there. – uchuugaka Aug 14 '13 at 09:21
  • I just want to draw a blue background. Once i see the code for blue, i can change to the other colors. I just want the cell to have a blue background. But not like how its displayed above, i was looking to have the whole cell blue, instead of only part of it. What code do i put in the drawRect? – Just a coder Aug 14 '13 at 21:53