I’ve found this code from Apple’s DragAndDropOutlineView example:
(newbie question at bottom) :-)
#import <Cocoa/Cocoa.h>
@interface ImageAndTextCell : NSTextFieldCell { @private NSImage *image; }
(NSImage *)image;
@end
and:
@implementation ImageAndTextCell
(void)dealloc { [image release]; image = nil; [super dealloc]; }
copyWithZone:(NSZone *)zone { ImageAndTextCell *cell = (ImageAndTextCell *)[super copyWithZone:zone]; cell->image = [image retain]; return cell; }
(void)setImage:(NSImage *)anImage { if (anImage != image) { [image release]; image = [anImage retain]; } }
(NSImage *)image { return image; }
(NSRect)imageFrameForCellFrame:(NSRect)cellFrame { if (image != nil) { NSRect imageFrame; imageFrame.size = [image size]; imageFrame.origin = cellFrame.origin; imageFrame.origin.x += 3; imageFrame.origin.y += ceil((cellFrame.size.height - imageFrame.size.height) / 2); return imageFrame; } else return NSZeroRect; }
(void)editWithFrame:(NSRect)aRect inView:(NSView *)controlView editor:(NSText *)textObj delegate:(id)anObject event:(NSEvent *)theEvent { NSRect textFrame, imageFrame; NSDivideRect (aRect, &imageFrame, &textFrame, 3 + [image size].width, NSMinXEdge); [super editWithFrame: textFrame inView: controlView editor:textObj delegate:anObject event: theEvent]; }
(void)selectWithFrame:(NSRect)aRect inView:(NSView *)controlView editor:(NSText *)textObj delegate:(id)anObject start:(int)selStart length:(int)selLength { NSRect textFrame, imageFrame; NSDivideRect (aRect, &imageFrame, &textFrame, 3 + [image size].width, NSMinXEdge); [super selectWithFrame: textFrame inView: controlView editor:textObj delegate:anObject start:selStart length:selLength]; }
(void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView { if (image != nil) { NSSize imageSize; NSRect imageFrame;
imageSize = [image size];
NSDivideRect(cellFrame, &imageFrame, &cellFrame, 3 + imageSize.width, NSMinXEdge);
if ([self drawsBackground]) {
self backgroundColor] set];
[[NSRectFill(imageFrame);
}
imageFrame.origin.x += 3;
imageFrame.size = imageSize;
if ([controlView isFlipped])
imageFrame.origin.y += ceil((cellFrame.size.height + imageFrame.size.height) / 2);
else
imageFrame.origin.y += ceil((cellFrame.size.height - imageFrame.size.height) / 2);
[image compositeToPoint:imageFrame.origin operation:NSCompositeSourceOver]; } [super drawWithFrame:cellFrame inView:controlView]; }
(NSSize)cellSize { NSSize cellSize = [super cellSize]; cellSize.width += (image ? [image size].width : 0) + 3; return cellSize; }
@end
My question is this: how exactly do I implement this? Right now, I’ve got another class that has only a few attributes and accessor methods. But I can’t figure out how the code above would work with my custom class (I’m assuming I might make an instance of the above in my class?).
I’m going to answer my own question… But, feel free to critique what I’ve got here; I’m sure there must be a better way.
Anyways, all you really need to do, is implement these 2 sets of code:
// add this to your - (void) awakFromNib method NSTableColumn *col = [self tableColumnWithIdentifier:@”whatever”]; [col setDataCell:[[[ImageAndTextCell alloc] init] autorelease]];
id cell; // implement this in your datasource cell = [aTableColumn dataCell]; [cell setImage:[NSImage imageNamed:@”OAHelpIcon.tiff”]];
But, make sure you import the previous code too…
ok it’s work fine but ….
lose the setting font for cell text
ideas ?
I was recently playing around on my own with this, and the way I did it was slightly different. I similarly made the Image
– BrianMoore
Have a little example ? TKS !
– Toni Moore