没有NIB的基于视图的NSOutlineView? [英] View-based NSOutlineView without NIB?

查看:34
本文介绍了没有NIB的基于视图的NSOutlineView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

NSOutlineViewNSTableView 的子类.目前,NSTableView 支持两种实现.

NSOutlineView is a subclass of NSTableView. And currently, NSTableView supports two implementations.

  • 基于细胞.
  • 基于视图.

要制作 OSX 10.8 Finder 样式侧栏(带有自动灰色图标样式),需要使用基于视图的表视图和源列表突出显示样式.

To make OSX 10.8 Finder style side bar (with automatic gray Icon styling), need to use view-based table view with source-list highlight style.

对于 NIB,这是典型的工作.没什么难的.(参见 SidebarDemo) 但我想避免使用任何 NIB 或 Interface Builder.我想纯粹以编程方式制作侧边栏.

With NIBs, this is typical job. Nothing hard. (see SidebarDemo) But I want to avoid any NIBs or Interface Builder. I want make the side bar purely programmatically.

在这种情况下,我遇到了大问题.AFAIK,无法为特定单元格提供原型视图.当我打开 .xib 文件时,我看到 包含 .这指定将用于该列的视图.我找不到如何使用公共 API 以编程方式进行设置.

In this case, I have big problem. AFAIK, there's no way to supply prototype view for specific cell. When I open .xib file, I see <tableColumn> is containing <prototypeCellViews>. And this specifies what view will be used for the column. I can't find how to set this programmatically using public API.

作为一种解决方法,我尝试使用 -[NSTableView makeViewWithIdentifier:owner:]-[NSTableView viewAtColumn:row:makeIfNecessary:] 手动制作单元格,但没有其中返回视图实例.我创建了一个 NSTableCellView,但它没有图像视图和文本字段实例.我也尝试设置它们,但字段被标记为 assign 因此实例立即释放.我试图通过强制保留它们来保留它,但它不起作用.NSTableView 不管理它们,所以我确信 table view 不喜欢我的实现.

As a workaround, I tried to make cell manually using -[NSTableView makeViewWithIdentifier:owner:] and -[NSTableView viewAtColumn:row:makeIfNecessary:], but none of them returns view instance. I created a NSTableCellView, but it doesn't have image-view and text-field instances. And I also tried to set them, but the fields are marked as assign so the instances deallocated immediately. I tried to keep it by forcing retaining them, but it doesn't work. NSTableView doesn't manage them, so I am sure that table view don't like my implementation.

我相信有一个属性可以为列设置这个原型视图.但我找不到他们.我在哪里可以找到该属性并以编程方式使系统默认 NSOutlineView 具有源列表样式?

I believe there's a property to set this prototype-view for a column. But I can't find them. Where can I find the property and make system-default NSOutlineView with source-list style programmatically?

推荐答案

如果您遵循 SidebarDemo 中的示例,它们将使用 NSTableCellView 的子类作为详细信息行.为了模拟InterfaceBuilder mojo,您可以在构造函数中将所有内容连接在一起.其余与演示相同(参见outlineView:viewForTableColumn:item:).

If you follow the example in SidebarDemo, they use a subclass of NSTableCellView for the detail rows. In order to emulate the InterfaceBuilder mojo, you can hook everything together in the constructor. The rest is the same as the demo (see outlineView:viewForTableColumn:item:).

@interface SCTableCellView : NSTableCellView
@end

@implementation SCTableCellView

- (id)initWithFrame:(NSRect)frameRect {
  self = [super initWithFrame:frameRect];
  [self setAutoresizingMask:NSViewWidthSizable];
  NSImageView* iv = [[NSImageView alloc] initWithFrame:NSMakeRect(0, 6, 16, 16)];
  NSTextField* tf = [[NSTextField alloc] initWithFrame:NSMakeRect(21, 6, 200, 14)];
  NSButton* btn = [[NSButton alloc] initWithFrame:NSMakeRect(0, 3, 16, 16)];
  [iv setImageScaling:NSImageScaleProportionallyUpOrDown];
  [iv setImageAlignment:NSImageAlignCenter];
  [tf setBordered:NO];
  [tf setDrawsBackground:NO];
  [[btn cell] setControlSize:NSSmallControlSize];
  [[btn cell] setBezelStyle:NSInlineBezelStyle];
  [[btn cell] setButtonType:NSMomentaryPushInButton];
  [[btn cell] setFont:[NSFont boldSystemFontOfSize:10]];
  [[btn cell] setAlignment:NSCenterTextAlignment];
  [self setImageView:iv];
  [self setTextField:tf];
  [self addSubview:iv];
  [self addSubview:tf];
  [self addSubview:btn];
  return self;
}

- (NSButton*)button {
  return [[self subviews] objectAtIndex:2];
}

- (void)viewWillDraw {
  [super viewWillDraw];
  NSButton* btn = [self button];
  ...

这篇关于没有NIB的基于视图的NSOutlineView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆