Display icon and text in one NSTableView cell

I would like to display an icon next to a text element in one cell of the table view.

An example of what I want to achieve is the list of applications in System Preferences → User Accounts → Login Elements.

What a good way?

+1
source share
1 answer

There is a good example here: http://www.cocoadev.com/index.pl?IconAndTextInTableCell You create your own NSCell that draws an 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 
+1
source

Source: https://habr.com/ru/post/1202606/


All Articles