Display icon and text in same cell of NSTableView

余生长醉 提交于 2019-12-08 07:53:33

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/3386164/display-icon-and-text-in-same-cell-of-nstableview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!