• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Alt. Background color and variable row height in NSTableView and NSOutlineView
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Alt. Background color and variable row height in NSTableView and NSOutlineView


  • Subject: Re: Alt. Background color and variable row height in NSTableView and NSOutlineView
  • From: Norbert Heger <email@hidden>
  • Date: Mon, 15 Oct 2001 12:41:21 +0200

Alykhan Jetha <email@hidden> asked:

> 1) I want to set alternating background row colors in a
> tableview and an outlineview. The only thing I've found is the
> ability to set the cell background color (and set the background
> color on the whole table), so I'd have to set the background
> color on each cell. It' seems like overkill to me. Am I missing
> some obvious method to set the color or return the background
> color for a given row?

Setting the background color of each cell in -tableView:willDisplayCell:...
won't work, since the cell frame doesn't cover the "intercellSpacing parts"
of the row, therefore these parts are not displayed with the proper
background color. So you have to use an NSTableView subclass instead,
overriding the -drawRow:clipRect: method (see the example below).

However, this doesn't solve another problem: The background of highlighted
table rows is drawn by the tableView (in -drawRow:clipRect:) AND by its
dataCells (in -drawWithFrame:inView:), but the cells do NOT use their
backgroundColor (if any) but the color returned from the cell's
-highlightColorWithFrame:inView: method. Therefore setting the background
color in -tableView:willDisplayCell:... wouldn't help either.

Unfortunately you can't set the cell's highlight color, instead you have to
implement a cell subclass that returns the proper color. Things get even
worse if you use lots of special dataCells in your table (checkmarks,
popups, etc.). You have to teach all of your cells to use the proper
background color when highlighted.

Best Regards, Norbert

_____________________________________________
Norbert Heger, Objective Development
http://www.obdev.at/




@interface NSObject (ODTableViewDelegate)
- (NSColor *)tableView:(NSTableView *)aTableView
backgroundColorForRow:(int)rowIndex;
@end

@interface ODTableView : NSTableView
{
struct {
unsigned int backgroundColorForRow:1;
} delegateRespondsTo;
}

@end

@implementation ODTableView

- (void)drawRow:(int)rowIndex clipRect:(NSRect)clipRect
{
if (![self isRowSelected:rowIndex]) {
id delegate = [self delegate];
if (delegateRespondsTo.backgroundColorForRow) {
NSColor *bgColor = [delegate tableView:self
backgroundColorForRow:rowIndex];

if (bgColor != nil) {
NSRect r = [self rectOfRow:rowIndex];
[bgColor set];
NSRectFill(NSIntersectionRect(r, clipRect));
}
}
}
[super drawRow:rowIndex clipRect:clipRect];
}

- (void)setDelegate:(id)aDelegate
{
[super setDelegate:aDelegate];
delegateRespondsTo.backgroundColorForRow =
[[self delegate]
respondsToSelector:@selector(tableView:backgroundColorForRow:)];
}

@end


References: 
 >Alt. Background color and variable row height in NSTableView and NSOutlineView (From: Alykhan Jetha <email@hidden>)

  • Prev by Date: Re: Little problem with NSFormatter way of working
  • Next by Date: Re: Displaying an alert box
  • Previous by thread: Re: Alt. Background color and variable row height in NSTableView and NSOutlineView
  • Next by thread: Additional info needed about "PackageMaker"
  • Index(es):
    • Date
    • Thread