Skip navigation.
 
mlRe: NSTableView - Alternating blue and white background
FROM : Joe Lester
DATE : Tue Dec 17 20:31:14 2002

Interesting. I use the same basic method that Dan Wood described in his November MacTech article in my NSTableView subclass. I've provided up to 8000 rows to my NSTableView subclass with no visible row color misalignment. Maybe I'm doing something different?

Here's my source. It's drifted some from the example in Dan Wood's article but my impression is that it should not really be that different. If there are rounding errors, I'd like to eliminate them, if possible:

- (void)highlightSelectionInClipRect:(NSRect)clipRect
/*"
    Overridden to draw the proper background color on the rows.
    Sends tableView:stripeColorForRow: to the data source to get
    the color for each visible row. If the data source does not
    respond, it stripes odd rows.
"*/
{
    //Get dims from self
    float rowHeight = [self rowHeight] + [self intercellSpacing].height;
    NSRect visibleRect = [self visibleRect];
   
    //Calculate the rect we should highlight
    NSRect highlightRect;
    highlightRect.origin = NSMakePoint(NSMinX(visibleRect), (int)(NSMinY(clipRect)/ rowHeight) * rowHeight);
    highlightRect.size = NSMakeSize(NSWidth(visibleRect), rowHeight );
   
    //Do the drawing
    while (NSMinY(highlightRect) < NSMaxY(clipRect))
    {
        NSColor *rowColor;
        NSRect clipedHighlightRect = NSIntersectionRect(highlightRect, clipRect);
        int row = (int)((NSMinY(highlightRect) + rowHeight / 2.0) / rowHeight);
       
        if ([self dataSource] &&
           [[self dataSource] respondsToSelector:@selector(tableView:stripeColorForRow:)])
        {
            NSColor *colorFromDataSource = [[self dataSource] tableView:self stripeColorForRow:row];
            rowColor = (colorFromDataSource) ? colorFromDataSource : [NSColor whiteColor];
        }
        else
        {
            rowColor = (0 == row % 2) ? [NSColor colorWithCalibratedRed:0.929 green:0.953 blue:0.996 alpha:1.0] : [NSColor whiteColor];
        }
       
        [rowColor set];
        NSRectFill(clipedHighlightRect);
        highlightRect.origin.y += rowHeight;
    }
   
    [super highlightSelectionInClipRect:clipRect];
}

Thanks!


>From: "Erik J. Barzeski" <<email_removed>>
>On 12/17/02 9:09am, "<email_removed>"
><<email_removed>> wrote:

>> One solution is described in some Mac Tech articles written by Dan
>> Wood. The code is available on his web site and IIRC, if you search the
>> archive you should find a post indicating the URL of the

>source codes.
>
>Unfortunately, this method is subject to rounding errors, so if you expect
>to have a few hundred (or more) rows in the table view, eventually the
>colors won't line up. You'll have a line that's half blue, half
>white, etc.
>
>Overriding the table cell drawing method is what we had to use
>in MailDrop.

_______________________________________________
cocoa-dev mailing list | <email_removed>
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

Related mailsAuthorDate
mlNSTableView - Alternating blue and white background Flemming Bengtsson Dec 17, 10:53
mlRe: NSTableView - Alternating blue and white background David Remahl Dec 17, 11:03
mlRe: NSTableView - Alternating blue and white background Warren.Burton Dec 17, 11:03
mlRe: NSTableView - Alternating blue and white background Cristi Savu Dec 17, 11:05
mlRe: NSTableView - Alternating blue and white background j o a r Dec 17, 11:15
mlRe: NSTableView - Alternating blue and white background j o a r Dec 17, 11:38
mlRe: NSTableView - Alternating blue and white background Stéphane Sudre Dec 17, 11:38
mlRe: NSTableView - Alternating blue and white background Erik J. Barzeski Dec 17, 16:04
mlRe: NSTableView - Alternating blue and white background Joe Lester Dec 17, 17:15
mlRe: NSTableView - Alternating blue and white background Stéphane Sudre Dec 17, 17:19
mlRe: NSTableView - Alternating blue and white background Erik J. Barzeski Dec 17, 18:50
mlRe: NSTableView - Alternating blue and white background Joe Lester Dec 17, 20:31
mlRe: NSTableView - Alternating blue and white background Erik J. Barzeski Dec 17, 20:51
mlRe: NSTableView - Alternating blue and white background John C. Randolph Dec 17, 20:53