2011-02-18 50 views
3

我我的大纲视图,我加入了自定义单元格,绘制自定义单元格,我指的示例代码,目前可可文档NSOutlineView更改公开图片

http://www.martinkahr.com/2007/05/04/nscell-image-and-text-sample/

我想改变公开的图像中我的自定义图像单元格的,我已经尝试下面的东西

- (void)outlineView:(NSOutlineView *)outlineView willDisplayCell:(id)cell forTableColumn:(NSTableColumn *)tableColumn item:(id)item 
    { 
     if([item isKindOfClass:[NSValue class]]) 
     { 
      MyData *pDt = (MyData *)[item pointerValue]; 
      if(pDt->isGroupElement()) 
      { 
       [cell setImage:pGroupImage]; 
      } 
     } 
} 

,但同样没有工作,有没有其他的办法来改变形象披露, 还我怎样才能找出willDispla yCell项目是否展开或折叠,所以我可以相应地设置图像,

这只是改变披露图像的地方吗?

+0

出于某种原因,我的大纲:willDisplayOutlineCell ....永远不会被调用。有什么建议么? – Tony 2012-01-02 05:09:25

+0

有关完整的自定义示例 - 请查看https://github.com/johndpope/TreeTest – johndpope 2015-02-19 05:56:55

回答

1

你有基本的想法,但你需要做的是自己画图像。下面的代码我使用:

- (void)outlineView:(NSOutlineView *)outlineView willDisplayOutlineCell:(id)cell forTableColumn:(NSTableColumn *)tableColumn item:(id)item { 
    NSString *theImageName; 
    NSInteger theCellValue = [cell integerValue]; 
    if (theCellValue==1) { 
     theImageName = @"PMOutlineCellOn"; 
    } else if (theCellValue==0) { 
     theImageName = @"PMOutlineCellOff"; 
    } else { 
     theImageName = @"PMOutlineCellMixed"; 
    } 

    NSImage *theImage = [NSImage imageNamed: theImageName]; 
    NSRect theFrame = [outlineView frameOfOutlineCellAtRow:[outlineView rowForItem: item]]; 
    theFrame.origin.y = theFrame.origin.y +17; 
    // adjust theFrame here to position your image 
    [theImage compositeToPoint: theFrame.origin operation:NSCompositeSourceOver]; 
    [cell setImagePosition: NSNoImage]; 
} 

您将需要3幅不同的图像,你可以看到,一个是ON状态,一个用于OFF状态,还一个是MIXED状态,这应该是介于两个之间。混合状态确保您仍然可以获得开幕式和闭幕式动画。

+0

已完成您的建议,但出现一些问题,无论项目是展开还是折叠,始终为cellvalue == 1,此代码为绘制图像在披露图片上,如果我的图片是透明的,那么它会显示披露图片 – Amitg2k12 2011-02-18 10:42:10

0

这是我曾尝试和迄今为止的工作,

/*,因为我们显示我们自己透露,扩大按钮*/

- (NSRect)frameOfOutlineCellAtRow:(NSInteger)row { 

    return NSZeroRect; 
} 
- (NSRect)frameOfCellAtColumn:(NSInteger)column row:(NSInteger)row { 
    NSRect superFrame = [super frameOfCellAtColumn:column row:row]; 

    if ((column == 0) && ([self isGroupItem:[self itemAtRow:row]])) { 
     return NSMakeRect(0, superFrame.origin.y, [self bounds].size.width, superFrame.size.height); 
    } 
    return superFrame; 
} 

我有子类NSOutlineView类,并且覆盖这些方法,

[self isGroupItem]是检查是否属于它的组。 但有一个问题,现在看起来像我需要做的鼠标操作:(在双击组行不会触发

4

自己研究这个问题,并尝试一些答案在这里,我发现其他方法提到将工作,但会要求你为了避免屏幕的文物和其他奇怪的行为进行更大量的人工干预。

,我发现最简单的办法是下面的,这在大多数情况下工作。

该解决方案还具有系统自动处理许多其他情况的附加好处,例如列移动等,无需您的参与EMENT。

- (void)outlineView:(NSOutlineView *)outlineView willDisplayOutlineCell:(id)cell 
    forTableColumn:(NSTableColumn *)tableColumn 
       item:(id)item 
{ 
    [cell setImage:[NSImage imageNamed: @"Navigation right 16x16 vWhite_tx"]]; 
    [cell setAlternateImage:[NSImage imageNamed: @"Navigation down 16x16 vWhite_tx"]]; 
} 

在你的情况,你会用你的类检测逻辑包装它,并为你的情况适当地设置细胞图像。

0

一个很好的方法来改变披露图像是使用视图根据大纲视图:

在您的视图控制器与NSOutlineViewDelegate:

- (NSView *)outlineView:(NSOutlineView *)outlineView viewForTableColumn:(NSTableColumn *)tableColumn item:(id)item 
{ 
    CustomNSTableCellView *cell = [outlineView makeViewWithIdentifier:tableColumn.identifier owner:self]; 
    cell.item      = item; 

    return cell; 
} 

你要继承你的NSOutlineView并在此改变方法:

- (id)makeViewWithIdentifier:(NSString *)identifier owner:(id)owner 
{ 
    id view = [super makeViewWithIdentifier:identifier owner:owner]; 

    if ([identifier isEqualToString:NSOutlineViewDisclosureButtonKey]) 
    { 
     // Do your customization 
     // return disclosure button view 

     [view setImage:[NSImage imageNamed:@"Disclosure_Categories_Plus"]]; 
     [view setAlternateImage:[NSImage imageNamed:@"Disclosure_Categories_Minus"]]; 
     [view setBordered:NO]; 
     [view setTitle:@""]; 

     return view; 
    } 

    return view; 
} 

//Frame of the disclosure view 
- (NSRect)frameOfOutlineCellAtRow:(NSInteger)row 
{ 
    NSRect frame = NSMakeRect(4, (row * 22), 19, 19); 
    return frame; 
} 
1

谁在寻找Swift2解决方案。子类NSRow的大纲视图和覆盖didAddSubview方法如下。

override func didAddSubview(subview: NSView) { 
    super.didAddSubview(subview) 
    if let sv = subview as? NSButton { 
     sv.image = NSImage(named:"IconNameForCollapsedState") 
     sv.alternateImage = NSImage(named:"IconNameForExpandedState") 
    } 
}