Re: Putting an Image Cell in NSTableView
Re: Putting an Image Cell in NSTableView
- Subject: Re: Putting an Image Cell in NSTableView
- From: Jeremy Dronfield <email@hidden>
- Date: Thu, 18 Jul 2002 19:40:32 +0100
On Wednesday, July 17, 2002, at 10:04 pm, email@hidden wrote
>
Set the column to display Images:
On Wednesday, July 17, 2002, at 10:30 pm, Chris Giordano wrote:
>
Anyway, just an idea. I've got something similar that I'll be coding
>
up one of these days if I ever get around to it. I'd love to know if
>
this actually works. It looks like it should, but then again, I've
>
never tried using images in a table view before. You might need to
>
return a copy of the image, rather than the image, but other than that,
>
it looks like it should work.
>
Thanks to both of you. After literally an hour's ceaseless searching, I
discovered the secret ingredient to make your suggestions work with the
setup I was using. When I click my "tag" button, the selected row cycles
its image through blue, grey, red blip. Here are the fruits of my
labours (ivar declarations and releases not necessarily included):
INITIALISING:
- (void)awakeFromNib
{
NSImage *blipBlue = [NSImage imageNamed:@"blipBlue.tiff"];
NSImage *blipGray = [NSImage imageNamed:@"blipGray.tiff"];
NSImage *blipRed = [NSImage imageNamed:@"blipRed.tiff"];
imageArray = [[NSArray alloc] initWithObjects:blipBlue, blipGray,
blipRed, nil];
// some irrelevant stuff here
if ([prefs arrayForKey:@"Marks"] != nil) {
bookmarks = [[NSMutableArray alloc] initWithArray:[prefs
objectForKey:@"Marks"]];
} else {
bookmarks = [[NSMutableArray alloc] init];
}
tPrototypeCell=[NSImageCell new];
tColumn = [tableView tableColumnWithIdentifier:@"tag"];
[tColumn setDataCell:tPrototypeCell];
}
CREATING A NEW ENTRY:
- (void)createBookmark
{
NSMutableDictionary *bookmark = [[NSMutableDictionary alloc]
init];
[tableView deselectRow:[tableView selectedRow]];
[bookmark setObject:[NSNumber numberWithInt:segmentValue]
forKey:@"scv"];
[bookmark setObject:[imageArray objectAtIndex:0] forKey:@"tag"];
[bookmark setObject:textToBookmark forKey:@"Bookmarks"];
[bookmarks insertObject:bookmark atIndex:0];
//[self saveData]; <= This has stopped working - I'm working on
a fix
[tableView reloadData];
[bookmark release];
}
MARKING THE SELECTED ENTRY:
- (IBAction)tagAction:(id)sender
{
NSString *theMark;
unsigned tag;
NSMutableDictionary *selectedBookmark = [[NSMutableDictionary alloc]
init];
int index = [tableView selectedRow];
id theRecord, theNote, theSegment;
theRecord = [bookmarks objectAtIndex:index];
theSegment = [theRecord objectForKey:@"scv"];
theNote = [theRecord objectForKey:@"tag"];
theMark = [theRecord objectForKey:@"Bookmarks"];
tag = [imageArray indexOfObjectIdenticalTo:theNote];
tag = (tag + 1) % 3;
[selectedBookmark setObject:theSegment forKey:@"scv"];
[selectedBookmark setObject:[imageArray objectAtIndex:tag]
forKey:@"tag"];
[selectedBookmark setObject:theMark forKey:@"Bookmarks"];
[bookmarks removeObjectAtIndex:index];
[bookmarks insertObject:selectedBookmark atIndex:index];
[selectedBookmark release];
[tableView reloadData];
//[self saveData]; <= Uh-oh.
}
AND SIMPLY THE BOG-STANDARD METHOD FOR PUTTING IT IN THE TABLE:
-(id)tableView:(NSTableView *)aTableView
objectValueForTableColumn:(NSTableColumn *)aTableColumn row:
(int)rowIndex
{
id theRecord, theValue;
NSParameterAssert(rowIndex >= 0 && rowIndex < [bookmarks count]);
theRecord = [bookmarks objectAtIndex:rowIndex];
theValue = [theRecord objectForKey:[aTableColumn identifier]];
return theValue;
}
Thanks again,
- Jeremy
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.