Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

dharma

macrumors newbie
Original poster
Feb 20, 2006
2
0
Hi Folks,

I'd like to insert image and text in the same cell in NSTableview, and the images are different in the different rows. What I know is to use a delegate method

-(void)tableView: WillDisplayCell: forTableColumn: row:
{

}

But I've no idea how I should implement the method. Would anyone help me please. I'm really new in Cocoa, so your detailed explanation is very helpful to me.

Thanks
 

caveman_uk

Guest
Feb 17, 2003
2,390
1
Hitchin, Herts, UK
From borkware http://www.borkware.com/quickies/one?topic=NSString
Putting an image into an attributed string
You'll need to use a text attachment.

Code:
- (NSAttributedString *) prettyName
{
    NSTextAttachment *attachment;
    attachment = [[[NSTextAttachment alloc] init] autorelease];
    NSCell *cell = [attachment attachmentCell];

    NSImage *icon = [self icon]; // or wherever you are getting your image
    [cell setImage: icon];

    NSString *name = [self name];
    NSAttributedString *attrname;
    attrname = [[NSAttributedString alloc] initWithString: name];

    NSMutableAttributedString *prettyName;
    prettyName = (id)[NSMutableAttributedString attributedStringWithAttachment:
                                                attachment]; // cast to quiet compiler warning
    [prettyName appendAttributedString: attrname];

    return (prettyName);

} // prettyName

This puts the image at the front of the string. To put the image in the middle of the string, you'll need to create an attributedstring with attachment, and then append that to your final attributed string.


This puts the image at the front of the string. To put the image in the middle of the string, you'll need to create an attributedstring with attachment, and then append that to your final attributed string.
I'd probably cache the attributed string somewhere so you don't have to generate everytime the tableview gets updated
 

caveman_uk

Guest
Feb 17, 2003
2,390
1
Hitchin, Herts, UK
Is that enough detail or do you need help on how to actually write the datasource/delegate methods? In this case I don't think you'd need to use the willDisplayCell method as I think you're OK just returning the NSAttributedString in the objectValueForTableColumn: row: datasource method. I've not actually done this though so I might be wrong. I have used willDisplayCell before for more complicated things with custom cells but as I said I don't think you need that here.
 

dharma

macrumors newbie
Original poster
Feb 20, 2006
2
0
Thanks caveman_uk for your help.:) It works, but the text sits at the bottom of the icon's height. It'll be perfect if the text is at the center height.
 

caveman_uk

Guest
Feb 17, 2003
2,390
1
Hitchin, Herts, UK
Ahhh, your icon is a lot bigger than the text size then. Do you want it that big? If not, resize it using NSImage's -setSize. Otherwise you'll need to create a custom NSCell class and handle all the drawing yourself. There's plenty of gotchas that await there :rolleyes: including the 'Thou must implement copyWithZone: properly' gotcha.

You could always cheat and actually show the icon and text in different table columns - just hide the column borders.
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
I think there might be another way. Can you not alter the baseline for the text part of the attributed string to move it up a bit?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.