# Adding image in a tableview



## flor (Dec 5, 2002)

I am desperatly trying to add an image to a tableview, but everything displays, except the image. Here is my code:

 NSImageCell 	*dataCell;
 NSTableColumn 	*column;

dataCell = [[NSImageCell alloc]initImageCell:[NSImage imageNamed"test.tiff"]];

column = [myTableView tableColumnWithIdentifier"Image"];
[dataCell setImageAlignment:NSImageAlignCenter];
[dataCell setImageScaling:NSScaleNone];
[dataCell setImageFrameStyle:NSImageFramePhoto];

[column setDataCell:dataCell];


What am i doing wrong. The border etc. displays fine, except for the image???

PS: The datacell contains the image, because i tried to draw it in a NSImageView with succes.

Thanks.


----------



## cbaron (Dec 10, 2002)

If your image is in your App's bundle, you'll have to load it from there using NSBundle 's function pathForResource: ofType:

```
- (NSString *)pathForResource: (NSString *)name ofType: (NSString *)extension
```
Returns the full pathname for the resource identified by name with the specified file extension. If the extension argument is nil or an empty string (@""), the resource sought is identified by name, with no extension. The method first looks for a nonlocalized resource in the immediate bundle directory; if the resource is not there, it looks for the resource in the language-specific ".lproj" directory (the local language is determined by user defaults).

The following code fragment gets the path to a localized sound, creates an NSSound instance from it, and plays the sound.


```
NSString *soundPath;
NSSound *thisSound;
NSBundle *thisBundle = [NSBundle bundleForClass:[self class]];
if (soundPath = [thisBundle pathForResource:@"Hello" ofType:@"snd"]) {
    thisSound = [[[NSSound alloc] initFromSoundfile:soundPath] autorelease];
    [thisSound play];
}
```

have fun.


----------



## anarchie (Dec 12, 2002)

cbaron:  That is not the problem at hand.  +imageNamed: searches for the image using NSBundle.  Like he said, the image displays fine in an NSImageView.  

BTW, there are also -pathForImageResource: and -pathForSoundResource: which can locate an image or sound resource.  With these methods, you do not need to specify "jpg" or "snd" - they locate a file with any recognized image or sound extension.  +imageNamed: will also search for a file with any known image extension if you do not provide one.

In the case of an NSTableView, you cannot specify a default image for cells in a particular column.  You must return that image in your -tableViewbjectValueForColumn:row: data source method.  It should be something like this:


```
- (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(int)row;
{
    if([[tableColumn identifier] isEqualToString:@"Image"])
    {
        //find correct image for this row maybe?
        return [NSImage imageNamed:@"test.tiff"];
    }
    //else return object for other columns/rows
}
```


----------

