I would like to display an icon next to a text item in an single cell of a table view.
An example of what I want to achieve is the application list in System Preferences -> User Accounts -> Login Items.
What's a good way?
There is a good example here how to do it: http://www.cocoadev.com/index.pl?IconAndTextInTableCell You make your own custom NSCell that both draws a image and text
@interface IconCell : NSCell
{
NSArray * cellValue;
}
- (void)setObjectValue:(id <NSCopying>)object;
- (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView;
@implementation IconCell
- (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
NSDictionary * textAttributes =
[NSDictionary dictionaryWithObjectsAndKeys:[NSFont
userFontOfSize:10.0],NSFontAttributeName, nil];
NSPoint cellPoint = cellFrame.origin;
[controlView lockFocus];
[[cellValue objectAtIndex:1] compositeToPoint:NSMakePoint(cellPoint.x+2,
cellPoint.y+14) operation:NSCompositeSourceOver];
[[cellValue objectAtIndex:0] drawAtPoint:NSMakePoint(cellPoint.x+18,
cellPoint.y) withAttributes:textAttributes];
[controlView unlockFocus];
}
- (void)setObjectValue:(id <NSCopying>)object
{
cellValue = (NSArray *)object;
}
@end