在NSTableView的同一单元格中显示图标和文本

时间:2010-08-02 08:31:09

标签: objective-c cocoa macos nstableview

我想在表格视图的单个单元格中的文本项旁边显示一个图标。

我想要实现的一个例子是系统偏好设置中的应用程序列表 - >用户帐户 - >登录项目。

什么是好方法?

1 个答案:

答案 0 :(得分:0)

这里有一个很好的例子:http://www.cocoadev.com/index.pl?IconAndTextInTableCell你制作自己的自定义NSCell,它们都绘制图像和文本

@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