18
votes

I have a view based nstableview. I want to color entire row based on some condtion for which I have used code below

- (NSTableRowView *)tableView:(NSTableView *)tableView rowViewForRow:(NSInteger)row 
{
    NSTableRowView *view = [[NSTableRowView alloc] initWithFrame:NSMakeRect(1, 1, 100, 50)];

    [view setBackgroundColor:[NSColor redColor]];
    return view;;
}

The delegate method is called, but table doesn't seem to be using NSTableRowView returned by delegate method.

Main aim here is coloring entire row based on some condition. Whats wrong in above implementation?

4
To set backgroundColor, you need to use - (void)tableView:(NSTableView *)tableView didAddRowView:(NSTableRowView *)rowView forRow:(NSInteger)row in your NSTableViewDelegate. See my more detailed answer below.DPlusV

4 Answers

39
votes

For anyone else who hits this and wants a custom NSTableRowView backgroundColor, there are two approaches.

  1. If you don't need custom drawing, simply set rowView.backgroundColor in - (void)tableView:(NSTableView *)tableView didAddRowView:(NSTableRowView *)rowView forRow:(NSInteger)row in your NSTableViewDelegate.

    Example:

    - (void)tableView:(NSTableView *)tableView
        didAddRowView:(NSTableRowView *)rowView
               forRow:(NSInteger)row {
    
        rowView.backgroundColor = [NSColor redColor];
    
    }
    
  2. If you do need custom drawing, create your own NSTableRowView subclass with desired drawRect. Then, implement the following in NSTableViewDelegate:

    Example:

    - (NSTableRowView *)tableView:(NSTableView *)tableView
                    rowViewForRow:(NSInteger)row {
        static NSString* const kRowIdentifier = @"RowView";
        MyRowViewSubclass* rowView = [tableView makeViewWithIdentifier:kRowIdentifier owner:self];
        if (!rowView) {
            // Size doesn't matter, the table will set it
            rowView = [[[MyRowViewSubclass alloc] initWithFrame:NSZeroRect] autorelease];
    
            // This seemingly magical line enables your view to be found
            // next time "makeViewWithIdentifier" is called.
            rowView.identifier = kRowIdentifier; 
        }
    
        // Can customize properties here. Note that customizing
        // 'backgroundColor' isn't going to work at this point since the table
        // will reset it later. Use 'didAddRow' to customize if desired.
    
        return rowView;
    }
    
1
votes

Finally it worked as below

    - (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row

{
        NSView *cellView = (NSView*) [tableView makeViewWithIdentifier:[tableColumn identifier] owner:[tableView delegate]];
        CALayer *viewLayer = [CALayer layer];
        [viewLayer setBackgroundColor:[[NSColor redcolor] CGColor]];
        [cellView setWantsLayer:YES]; 
        [cellView setLayer:viewLayer];
        return cellView;
    }

Please note.. u need to convert nscolor to cgcolor which you can find in https://gist.github.com/707921 or http://forrst.com/posts/CGColor_Additions_for_NSColor-1eW

0
votes

If you watch the presentation on view based tableviews from WWDC 2011, you'll see that the main idea is to create the views in Interface Builder, and then obtain them from there. Something like:

[tableView makeViewWithIdentifier:@"GroupRow" owner:self];

Once you have obtained the view, just set its properties and return it.

Notice in this example that it has its own identifier, so remember to set that, but you can also used automatic identifiers.

I don't know if a direct link to the WWDC will work, but the main page is here: https://developer.apple.com/videos/wwdc/2011/ and if you search for "View Based NSTableView Basic to Advanced", you'll find it. It is well worth watching.

0
votes

I re-wrote the layer approach. In Swift 3.2

func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {

    let greenCell = self.tableview.make(withIdentifier: "green", owner: self)
    let layer:CALayer = CALayer()
    layer.backgroundColor = NSColor.green.cgColor

    greenCell?.wantsLayer = true
    greenCell?.layer = layer

    return greenCell

}

Don't forget to change the Identifier of the cell according to your storyboard, and in the code identifier "green". And surely, the background color if you want.