本文介绍了在NSTableView的同一单元格中显示图标和文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在表格视图的单个单元格中的文本项旁边显示一个图标。
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.
有什么好办法?
推荐答案
这里有一个很好的例子:您制作自己的自定义NSCell,既绘制图像和文本
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
这篇关于在NSTableView的同一单元格中显示图标和文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!