Re: Color/Number List
Re: Color/Number List
- Subject: Re: Color/Number List
- From: Tom Waters <email@hidden>
- Date: Thu, 26 Jul 2001 22:24:59 -0700
On Thursday, July 26, 2001, at 09:56 PM, Joshua D. Orr wrote:
>
It does not appear to work, the cells do not change to the correct
>
color (but the numbers do get displayed). Does anyone have any idea
>
what's wrong?
>
>
On Wednesday, July 25, 2001, at 08:49 PM, Tom Waters wrote:
>
> On Wednesday, July 25, 2001, at 04:26 PM, Joshua D. Orr wrote:
>
>> I have a list (actually an array) of NSNumber's and NSColor's. The
>
>> colors
>
>> correspond to the numbers. I want to be able to somehow display
>
>> these in a
>
>> list (like a table).
>
> This is written in email and not tested... but it should point you in
>
> the right direction.
>
>
>
> assumes that your arrays are allocated and filled somewhere else...
>
> it also assumes you have one column in your table and that that
>
> columns dataCell prototype is an NSTextFieldCell
>
> and that you've set the instance of MyDelegate as the table's
>
> dataSource and delegate in IB.
>
>
>
> <previous example snipped>
Sorry, I forgot to mention that NSTextFieldCell's don't draw a
background by default...
You can call [cell setDrawsBackground:YES] in the willDisplayCell method
every time, or you can set it once in your awakeFromNib by grabbing the
column from the table and then grabbing the column's dataCell and
setting it there.
[[[[table tableColumns] objectAtIndex:0] dataCell]
setDrawsBackground:YES];
Here's the same example I sent you before with the latter change. same
provisos about the IB setup, except you need to also have the delegate
have an outlet connected to the table.
#import <Cocoa/Cocoa.h>
@interface TableDelegate : NSObject
{
NSArray *numbers;
NSArray *colors;
IBOutlet id table;
}
@end
@implementation TableDelegate
- (void)awakeFromNib
{
numbers = [NSArray arrayWithObjects:
[NSNumber numberWithInt: 42],
[NSNumber numberWithInt: 43],
[NSNumber numberWithInt: 44],
[NSNumber numberWithInt: 45],
[NSNumber numberWithInt: 46],
nil];
colors = [NSArray arrayWithObjects:
[NSColor redColor],
[NSColor blueColor],
[NSColor greenColor],
[NSColor yellowColor],
[NSColor cyanColor],
nil];
[numbers retain];
[colors retain];
[[[[table tableColumns] objectAtIndex:0] dataCell]
setDrawsBackground:YES];
}
- (void)tableView:(NSTableView *)view
willDisplayCell:(id)cell
forTableColumn:(NSTableColumn *)col
row:(int)row
{
[cell setBackgroundColor: [colors objectAtIndex:row]];
}
- (int)numberOfRowsInTableView:(NSTableView *)view
{
return [numbers count];
}
- (id)tableView:(NSTableView *)view
objectValueForTableColumn:(NSTableColumn *)col
row:(int)row
{
return [numbers objectAtIndex:row];
}
@end