PDA

View Full Version : Inserting an image & text in NSTableView




dharma
Feb 20, 2006, 05:14 PM
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
Feb 21, 2006, 02:40 AM
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.


- (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
Feb 21, 2006, 02:52 AM
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
Feb 21, 2006, 08:00 AM
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
Feb 21, 2006, 08:55 AM
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.

caveman_uk
Feb 21, 2006, 09:03 AM
Incidently, I've just noticed the code I posted earlier has a memory leak - attrname needs to be released before the return.

robbieduncan
Feb 21, 2006, 09:10 AM
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?