Custom background in NSTableView based view

I have custom row color variables in my view-based NSTableRowView , overriding NSTableRowView -drawBackgroundInRect: This works for the most part in that the colors of the cells themselves change, but this, obviously, does not affect the background of the table view itself (for example, when the scroll image bounces). Screenshot:

NSTableView background

What is the best way to configure this? I posted a question earlier about the same issue, but with tabular representations of a honeycomb table . The solution I found does not seem to work with view-based views.

+6
source share
3 answers

Have you tried overriding NSTableView drawBackgroundInClipRect:(NSRect)clipRect

+3
source

There is a video about the conversation “View Based NSTableView Basic to Advanced” (available here ), in which the background below the last line is drawn.

To extend this technique, you can subclass NSTableView and add a little code:

 // somewhere in your setup code (colors just intended as examples): tableView.colors = [NSArray arrayWithObjects: [NSColor lightGrayColor],[NSColor grayColor], nil]; // In the table view subclass: -(void)drawBackgroundInClipRect:(NSRect)clipRect { // The super class implementation obviously does something more // than just drawing the striped background, because // if you leave this out it looks funny [super drawBackgroundInClipRect:clipRect]; NSRect boundsToDraw = clipRect; CGFloat yStart = 0; NSInteger rowIndex = -1; if ( clipRect.origin.y < 0 ) { while (yStart > NSMinY(boundsToDraw)) { CGFloat yRowTop = yStart - self.rowHeight; NSRect rowFrame = NSMakeRect(0, yRowTop, boundsToDraw.size.width, self.rowHeight); NSUInteger colorIndex = rowIndex % self.colors.count; NSColor *color = [self.colors objectAtIndex:colorIndex]; [color set]; NSRectFill(rowFrame); yStart -= self.rowHeight; rowIndex--; } } } 
+2
source

I have not tried it, but what happens if you just override -drawRect :?

-1
source

Source: https://habr.com/ru/post/904635/


All Articles