代码之家  ›  专栏  ›  技术社区  ›  Nick Moore Wain

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

  •  1
  • Nick Moore Wain  · 技术社区  · 14 年前

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

    我想实现的一个示例是系统首选项中的应用程序列表->用户帐户->登录项。

    什么是好办法?

    1 回复  |  直到 14 年前
        1
  •  0
  •   Jonas Jongejan    14 年前

    这里有一个很好的例子: 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