Re: Icons in NSTableView?
Re: Icons in NSTableView?
- Subject: Re: Icons in NSTableView?
- From: Tom Waters <email@hidden>
- Date: Sun, 10 Jun 2001 14:57:52 -0700
On Sunday, June 10, 2001, at 11:32 AM, Matt Ridley wrote:
>
Can an NSTableView display icons in one of its columns?
>
>
If not, which control would be a more appropriate UI element to use for
>
the
>
display of a non-hierarchical, three-column table-like structure, with
>
the
>
first column containing an image and the other two columns each
>
containing
>
plain text?
>
>
Also, does anyone happen to have sample code for anything like the
>
above? I
>
seem to recall reading here recently that someone had a sample project
>
for
>
this very thing, but I can't locate the email.
>
>
Thanks for any help. :-)
Here's something I just threw together to demonstrate this... (not
optimal code, but it should answer your question)
In your real code, you'll want to change
tableView:objectValueForTableColumn:row: to return the appropriate
images and strings for each row.
Here's how to set up the nib for this example: (It sure would be nice
if objects.nib was in XML like classes.nib and info.nib so we could just
include them in emails!)
Put a table view in the window.
Give it three columns.
Set the column identifiers set to "image", "name" and "desc".
Set File's Owner's custom class to ImageInTable
Add a single outlet called "table" to File's Owner.
Connect that outlet to the table you created above.
Connect the table's datasource outlet to File's Owner.
Put this code in ImageInTable.m
#import <Cocoa/Cocoa.h>
@interface ImageInTable : NSApplication
{
NSImage *image;
IBOutlet id table;
}
@end
@implementation ImageInTable
- (void)awakeFromNib
{
id cell;
image = [NSImage imageNamed: @"NSApplicationIcon"];
cell = [[NSImageCell alloc] initImageCell: image];
[[table tableColumnWithIdentifier: @"image"] setDataCell: cell];
}
- (int)numberOfRowsInTableView:(NSTableView *)view
{
return 10;
}
- (id)tableView:(NSTableView *)view
objectValueForTableColumn:(NSTableColumn *)col
row:(int)row
{
if ([[col identifier] isEqualTo: @"image"]) {
return image;
} else if ([[col identifier] isEqualTo: @"name"]) {
return @"Name";
} else if ([[col identifier] isEqualTo: @"desc"]) {
return @"Description";
}
return @"";
}
@end
[I put this project (4,578 bytes) up on my site at
http://www.whidbeysoft.com/downloads/ImageInTable.tar.gz]