Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > Mac OS X Development Forums > Mac OS X Development

Reply
 
LinkBack Thread Tools Display Modes
Old 01-16-2011, 07:58 AM   #1 (permalink)
Registered Member
 
Join Date: Sep 2008
Posts: 169
Danneman is on a distinguished road
Default Images in tableview cells w. ImageAndTextCells.h

I'm trying to add an image to the cells of a tableview using the Cocoa Extensionclass "ImageAndTextCell.h".

I've studied the "ImageBackground"-sample (found here: Loading…) and set my code up in the following manner:

.m-file:
Code:
#import "ImageAndTextCell.h"

....

- (void)awakeFromNib 
{
	NSTableColumn* tableColumn = [tableView tableColumnWithIdentifier:@"MenuColumn"];
	ImageAndTextCell* imageAndTextCell = [[[ImageAndTextCell alloc] init] autorelease];
	[tableColumn setDataCell:imageAndTextCell];
}

...

- (void)tableView: (NSTableView *)tableView willDisplayCell: (id)cell forTableColumn: (NSTableColumn *)tableColumn row: (int)row
{
	NSImage *img = [[NSImage alloc] initWithContentsOfFile:@"myIcon.png"];
	[(ImageAndTextCell*)cell setImage:img];
}
But instead of an icon being set in addition to the text, only the text is being displayed.

Perhaps I'm not getting the tablecolumn appropriately? It is set in the xib-file to have both name and identifier "MenuColumn". Should it really be referenced using a string?

Any ideas?

Last edited by Danneman; 01-16-2011 at 08:15 AM.
Danneman is offline   Reply With Quote
Old 01-16-2011, 03:22 PM   #2 (permalink)
Registered Member
 
Join Date: Sep 2008
Posts: 169
Danneman is on a distinguished road
Default

Never let it be said I'm a quitter - 3'd day and finally got it working *phew*.

Two solutions finally worked:


SOLUTION 1: Changing NSCells to NSBrowserCells

Code:
....

- (void)awakeFromNib 
{
	NSTableColumn* col = [[tableView tableColumns] objectAtIndex: 0]; // Only have one col so this is ok
	NSBrowserCell* cell = [[NSBrowserCell alloc] init];
	[cell setLeaf: YES];
	[col setDataCell: cell];
}

....

- (void)tableView: (NSTableView *)tableView willDisplayCell: (id)cell forTableColumn: (NSTableColumn *)tableColumn row: (int)row 
{
	NSImage *img = [NSImage imageNamed:@"myIcon.png"];
	[(NSBrowserCell*)cell setImage:img];	
}

SOLUTION 2: Changing NSCells to ImageAndTextCells.

This requires the inclusion of the "ImageAndTextCell.h/m".

Code:
#import "ImageAndTextCell.h"

....

- (void)awakeFromNib 
{
	NSTableColumn* tableColumn = [tableView tableColumnWithIdentifier:@"MenuColumn"];
	ImageAndTextCell* imageAndTextCell = [[[ImageAndTextCell alloc] init] autorelease];
	[tableColumn setDataCell:imageAndTextCell];
}

....

- (void)tableView: (NSTableView *)tableView willDisplayCell: (id)cell forTableColumn: (NSTableColumn *)tableColumn row: (int)row 
{
	NSImage *img = [NSImage imageNamed:@"myIcon.png"];
	[(ImageAndTextCell*)cell setImage:img];	
}

There you go The codesample "ImageBackground" was crucial, although I'm generally amazed at the poor documentation of all samples that Apple provide, making it harder than necessary for newbies like me trying to learn to discern any understanding from them.

Wonder which one is the best to use, though. Have read some posts from people recommending not to use the NSBrowserCell, but giving little input as to why...
Danneman is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
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
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 373
8 members and 365 guests
husthlj, illogical, LegionMD, LunarMoon, mer10, Murphy, padsoftware, stanny
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,677
Threads: 94,127
Posts: 402,916
Top Poster: BrianSlick (7,990)
Welcome to our newest member, husthlj
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 07:11 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0