How to make lines of different color in my NSTableView ?
09/07/03 22:51
I want to make my NSTableView more user friendly by alternating or setting a specific background color for each line. How ?
To achieve this basic effect is simple:
first, you need to set a delegate for your table view, either Interface Builder either programmaticaly.
Then, your delegate class should implement the following instance method:
- (void)tableView:(NSTableView *)aTableView willDisplayCell:(id)aCell forTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex
{
}
In the body of the method, you can use [aCell setDrawsBackground:YES] and [aCell setBackgroundColor:myColor] and any other call affecting the display of cell (see NSCell documentation). Just don't forget that you have to set the drawing parameters for EVERY cell, not only the ones you want to be different from the default setting. This is because there is only one NSCell per column, so the modifications you are doing affect every cell in the same column that will be drawn after your change…
Example:
if (rowIndex & 1) {
[aCell setDrawsBackground:YES] ;
[aCell setBackgroundColor:[NSColor colorWithCalibratedRed:200.0/255.0 green:217.0/255.0 blue:1.0 alpha:1.0]] ;
}
else {
[aCell setDrawsBackground:NO] ;
[aCell setBackgroundColor:[NSColor whiteColor]] ;
}
Note that there is no UI rule to specify if odd lines have to be colored instead of even ones, so feel free to do as you like…
If you use two different colors other than the background color of your lists, you will notice that the background color show up between cells.
If you want to avoid that, then overwrite - (void)drawRow:(int)rowIndex clipRect:(NSRect)clipRect of NSTableView instead.
first, you need to set a delegate for your table view, either Interface Builder either programmaticaly.
Then, your delegate class should implement the following instance method:
- (void)tableView:(NSTableView *)aTableView willDisplayCell:(id)aCell forTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex
{
}
In the body of the method, you can use [aCell setDrawsBackground:YES] and [aCell setBackgroundColor:myColor] and any other call affecting the display of cell (see NSCell documentation). Just don't forget that you have to set the drawing parameters for EVERY cell, not only the ones you want to be different from the default setting. This is because there is only one NSCell per column, so the modifications you are doing affect every cell in the same column that will be drawn after your change…
Example:
if (rowIndex & 1) {
[aCell setDrawsBackground:YES] ;
[aCell setBackgroundColor:[NSColor colorWithCalibratedRed:200.0/255.0 green:217.0/255.0 blue:1.0 alpha:1.0]] ;
}
else {
[aCell setDrawsBackground:NO] ;
[aCell setBackgroundColor:[NSColor whiteColor]] ;
}
Note that there is no UI rule to specify if odd lines have to be colored instead of even ones, so feel free to do as you like…
If you use two different colors other than the background color of your lists, you will notice that the background color show up between cells.
If you want to avoid that, then overwrite - (void)drawRow:(int)rowIndex clipRect:(NSRect)clipRect of NSTableView instead.