Re: Drawing the background of a single row in NSTableView
Re: Drawing the background of a single row in NSTableView
- Subject: Re: Drawing the background of a single row in NSTableView
- From: "Adam R. Maxwell" <email@hidden>
- Date: Wed, 01 Jul 2009 18:57:07 -0700
On Jul 1, 2009, at 6:40 PM, Graham Cox wrote:
On 02/07/2009, at 10:09 AM, Peter Zegelin wrote:
Problem with this is there is no obvious concordance ( that I can
see) between a row and its rectangle.
Wouldn't scrolling affect all this?
I wonder if the delegate method:
- (void)tableView:(NSTableView *)aTableView willDisplayCell:(id)
aCell forTableColumn:(NSTableColumn *)aTableColumnrow:(NSInteger)
rowIndex
would be better for your needs? It gives you the row and column so
you know where you are, and is called as part of the drawing loop of
the table, you can just go ahead and draw. Use -rectOfRow to get the
row and paint the background. I just tried the code below and it
works fine:
ISTR having problems with this, possibly due to intercell spacing,
back when we had to draw alternating row colors manually.
Another alternative is to subclass NSTableView and override -
drawRow:clipRect: which might be considered less of a hack than
using the delegate method to draw.
I took that route, using an additional datasource method that I've
defined as tableView:backgroundColorForRow:, and it seems to work
fairly well.
- (void)drawRow:(NSInteger)row clipRect:(NSRect)clipRect;
{
NSColor *color = [[self dataSource] tableView:self
backgroundColorForRow:row];
// ignore any background color if the row is selected
if (color && [self isRowSelected:row] == NO) {
[NSGraphicsContext saveGraphicsState];
NSRectClip(clipRect);
NSRect rowRect = [self rectOfRow:row];
// draw over the alternating row color
[[NSColor whiteColor] setFill];
NSRectFill(NSIntersectionRect(rowRect, clipRect));
// draw with rounded end caps
CGFloat radius = NSHeight(rowRect) / 2.0;
NSBezierPath *p = [NSBezierPath
bezierPathWithRoundedRect:NSInsetRect(rowRect, 1.0, 0.0)
xRadius:radius yRadius:radius];
[color setFill];
[p fill];
[NSGraphicsContext restoreGraphicsState];
}
// draw cells on top of the new row background
[super drawRow:row clipRect:clipRect];
}
Attachment:
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden