Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Feb 20, 2006, 05:14 PM   #1
dharma
macrumors newbie
 
Join Date: Feb 2006
Inserting an image & text in NSTableView

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
dharma is offline   0 Reply With Quote
Old Feb 21, 2006, 02:40 AM   #2
caveman_uk
Guest
 
caveman_uk's Avatar
 
Join Date: Feb 2003
Location: Hitchin, Herts, UK
From borkware http://www.borkware.com/quickies/one?topic=NSString
Quote:
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 is offline   0 Reply With Quote
Old Feb 21, 2006, 02:52 AM   #3
caveman_uk
Guest
 
caveman_uk's Avatar
 
Join Date: Feb 2003
Location: 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.
caveman_uk is offline   0 Reply With Quote
Old Feb 21, 2006, 08:00 AM   #4
dharma
Thread Starter
macrumors newbie
 
Join Date: Feb 2006
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.
dharma is offline   0 Reply With Quote
Old Feb 21, 2006, 08:55 AM   #5
caveman_uk
Guest
 
caveman_uk's Avatar
 
Join Date: Feb 2003
Location: 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 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 is offline   0 Reply With Quote
Old Feb 21, 2006, 09:03 AM   #6
caveman_uk
Guest
 
caveman_uk's Avatar
 
Join Date: Feb 2003
Location: Hitchin, Herts, UK
Incidently, I've just noticed the code I posted earlier has a memory leak - attrname needs to be released before the return.
caveman_uk is offline   0 Reply With Quote
Old Feb 21, 2006, 09:10 AM   #7
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
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?
robbieduncan is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
Inserting an image into a word Mac omansour MacBook Pro 2 Apr 13, 2011 07:30 AM
Need to Search image & text (same cell) in tableView Nekbeth iPhone/iPad Programming 0 Mar 9, 2011 06:44 PM
Inserting an image into a blog redshift1 Web Design and Development 4 Oct 18, 2010 01:48 PM
How to insert an image in a forum like this one shirlparker iPad 1 Jun 14, 2010 12:26 PM
vertically aligning text in NSTableView row Monaj Mac Programming 2 Jan 20, 2010 12:12 PM


All times are GMT -5. The time now is 07:00 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC