Re: Getting the blue and white table views
Re: Getting the blue and white table views
- Subject: Re: Getting the blue and white table views
- From: j o a r <email@hidden>
- Date: Mon, 7 Apr 2003 00:36:12 +0200
I don't know how many times I will have to respond to this question on
this list... :(
You cannot create good alternating row colors in table views without
subclassing. It's not difficult, and I'll include source at the end of
this email (since it is apparent that using the list archives is too
difficult for our readers).
The reason why you cannot use the delegate methods to set the color is
that:
1) It will only color your cells, and you often have fewer rows than
the table view is tall - thus leaving a ghastly non-striped portion at
the bottom of the table view.
2) Since it will only color your cells, it will not color the spaces
between cells. You can work around this by setting the
inter-cell-spacing to 0, but why bother?
j o a r
Ps. I can take no credit for the source snippet below as it comes from
Apple's MP3Player sample code. If I have introduced some errors in that
code then those errors are mine and not Apple's of course.
On Sunday, Apr 6, 2003, at 23:52 Europe/Stockholm, Harriet Kerry wrote:
Joar's answer is a good one, but you might spend a day
there. Look instead for delegate stuff for the table
view, where you have a chance to intervene before it
actually displays a row.
The list of delegate notifications is not long; the
prospect of dividing the row number for its modulo to
see if it is going to be blue or white is not
insurmountable; and your own documentation at
/Developer/Documentation/Cocoa/Reference
if read thoroughly, should give you all the clues you
need.
=================================================
#import <Cocoa/Cocoa.h>
@interface StripedTableView : NSTableView
@end
=================================================
#import "NSTableViewAdditions.h"
@interface NSColor (Additions)
+ (NSColor *) tableViewStripeColor;
@end
@implementation NSColor (Additions)
// Light blue
#define STRIPE_RED (237.0 / 255.0)
#define STRIPE_GREEN (243.0 / 255.0)
#define STRIPE_BLUE (254.0 / 255.0)
+ (NSColor *) tableViewStripeColor
{
static NSColor *stripeColor = nil;
if (stripeColor == nil)
{
stripeColor = [[NSColor colorWithCalibratedRed: STRIPE_RED
green: STRIPE_GREEN
blue: STRIPE_BLUE
alpha: 1.0] retain];
}
return stripeColor;
}
@end
@implementation StripedTableView
// Code to draw the striped background. Borrowed from Apple's MP3Player
sample code
- (void) drawStripesInRect:(NSRect) clipRect
{
NSRect stripeRect;
float fullRowHeight = [self rowHeight] + [self
intercellSpacing].height;
float clipBottom = NSMaxY(clipRect);
int firstStripe = clipRect.origin.y / fullRowHeight;
// we're only interested in drawing the stripes
if (firstStripe % 2 == 0)
{
firstStripe++;
}
// set up first rect
stripeRect.origin.x = clipRect.origin.x;
stripeRect.origin.y = firstStripe * fullRowHeight;
stripeRect.size.width = clipRect.size.width;
stripeRect.size.height = fullRowHeight;
// set the color
[[NSColor tableViewStripeColor] set];
// and draw the stripes
while (stripeRect.origin.y < clipBottom)
{
NSRectFill(stripeRect);
stripeRect.origin.y += fullRowHeight * 2.0;
}
}
- (void) highlightSelectionInClipRect:(NSRect) rect
{
[self drawStripesInRect: rect];
[super highlightSelectionInClipRect: rect];
}
@end
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.