The last day or so I've been reading various approaches on how to add an image in a tableview's cell (with the text remaining).
APPROACH 1:
According to the NSCell Class Reference, it should be as easy as using the "setImage"-function:
This however results in cells without text nor image (empty cell).
APPROACH 2:
Reading some more in the docs, you come across the "initImageCell" which returns a cell that is initialized with an image. So I tried converting the current cell to an image-cell by using this code:
This also results in cells without text nor image (empty cell).
Also, the cell-type continues to be "NSTextFieldCell", so obviously the object-cast did not work.
APPROACH 3:
Since the reference docs did not give me what I needed (or I did not understand them in order to discern what I needed from them - which admittedly is the more probable case), I tried reading some of the code-samples apple provides.
I came across a Cocoa Extension class called "ImageAndTextCell.h", which I included in my project and referenced using this code that I swiped from the samples, and hopefully should convert the cell to an ImageAndTextCell-type with the image in question:
This code only resulted in the text appearing, without an image. So no change at all.
And to top it off, the class of the cell continues to be "NSTextFieldCell".
Well, that's the three most coherent approaches I've tried without any success so far (there's been plenty more hair-pulling-inducing attempts, like constructing a view w image and adding it to the cell, but I'll leave it at that). Any ideas?
APPROACH 1:
According to the NSCell Class Reference, it should be as easy as using the "setImage"-function:
Code:
- (void)tableView: (NSTableView *)tableView willDisplayCell: (id)cell forTableColumn: (NSTableColumn *)tableColumn row: (int)row
{
[cell setImage:[NSImage imageNamed:@"myIcon.png"]];
}
This however results in cells without text nor image (empty cell).
APPROACH 2:
Reading some more in the docs, you come across the "initImageCell" which returns a cell that is initialized with an image. So I tried converting the current cell to an image-cell by using this code:
Code:
- (void)tableView: (NSTableView *)tableView willDisplayCell: (id)cell forTableColumn: (NSTableColumn *)tableColumn row: (int)row
{
(NSImageCell*)cell = [cell initImageCell:[NSImage imageNamed:@"myIcon.png"]];
}
This also results in cells without text nor image (empty cell).
Also, the cell-type continues to be "NSTextFieldCell", so obviously the object-cast did not work.
APPROACH 3:
Since the reference docs did not give me what I needed (or I did not understand them in order to discern what I needed from them - which admittedly is the more probable case), I tried reading some of the code-samples apple provides.
I came across a Cocoa Extension class called "ImageAndTextCell.h", which I included in my project and referenced using this code that I swiped from the samples, and hopefully should convert the cell to an ImageAndTextCell-type with the image in question:
Code:
#import "ImageAndTextCell.h"
....
- (void)tableView: (NSTableView *)tableView willDisplayCell: (id)cell forTableColumn: (NSTableColumn *)tableColumn row: (int)row
{
NSImage *image1 = [[NSImage alloc] initWithContentsOfFile:@"myIcon.png"];
[(ImageAndTextCell*)cell setImage:image1];
}
This code only resulted in the text appearing, without an image. So no change at all.
And to top it off, the class of the cell continues to be "NSTextFieldCell".
Well, that's the three most coherent approaches I've tried without any success so far (there's been plenty more hair-pulling-inducing attempts, like constructing a view w image and adding it to the cell, but I'll leave it at that). Any ideas?