以编程方式创建 NSMatrix,Snow Leopard

发布于 2024-08-12 03:50:22 字数 956 浏览 7 评论 0原文

我正在尝试以编程方式创建 NSImageCell 原型的 NSMatrix (如果您可以通过 Interface Builder 帮助我完成此操作,请成为我的嘉宾来回答我 此处

我创建了一个新的空项目。其中,普通对象(NSObject 的子类)具有以下代码(并且只有此代码):

- (void) awakeFromNib {
    NSRect f = view.bounds;
    NSMatrix* matrix = [[NSMatrix alloc] initWithFrame:f mode:NSListModeMatrix cellClass:[NSImageCell class] numberOfRows:1 numberOfColumns:1];

    [matrix setCellSize:NSMakeSize(100, 100)];
    [matrix sizeToCells];
    [matrix setNeedsDisplay:YES];

    [view addSubview:[matrix autorelease]];

    [view setNeedsDisplay:YES];

    NSLog(@"Matrix frame: %@", NSStringFromRect(matrix.frame));
}

“view”是指定为出口的 NSView,连接到 Interface Builder 中 Window 的 Content View。

矩阵根本不显示。正如预期的那样,它打印并记录帧大小为 (0,0,100,100)。

我做错了什么?

正如标题所述,我在 Snow Leopard 机器上使用 Xcode 3.2.1 执行此操作。

I'm trying to create an NSMatrix of an NSImageCell prototype programmatically (if you can help me doing it via Interface Builder, be my guest at answering me here)

I created a new, empty project. In it, a normal object (subclassing NSObject) has this code (and only this code):

- (void) awakeFromNib {
    NSRect f = view.bounds;
    NSMatrix* matrix = [[NSMatrix alloc] initWithFrame:f mode:NSListModeMatrix cellClass:[NSImageCell class] numberOfRows:1 numberOfColumns:1];

    [matrix setCellSize:NSMakeSize(100, 100)];
    [matrix sizeToCells];
    [matrix setNeedsDisplay:YES];

    [view addSubview:[matrix autorelease]];

    [view setNeedsDisplay:YES];

    NSLog(@"Matrix frame: %@", NSStringFromRect(matrix.frame));
}

"view" is an NSView specified as an outlet, connected to the Content View of the Window in Interface Builder.

The matrix does not show at all. It prints to log that the frame size is (0,0,100,100), as expected.

What am I doing wrong?

As the title states, I'm doing this on a Snow Leopard machine, using Xcode 3.2.1.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

才能让你更想念 2024-08-19 03:50:22

NSMatrix 默认情况下不会绘制任何内容,因为它是 NSCell 的容器。您可以通过将 NSMatrix 设置为绘制背景来验证它是否已正确添加到视图中。

[matrix setBackgroundColor:[NSColor blueColor]];
[matrix setDrawsBackground:YES];

由于 NSMatrixNSCell 的容器,因此您需要用一些东西填充它们。在您发布的示例中,您可以通过获取与唯一的行和列对应的单元格并设置图像来完成此操作。

NSImageCell *cell = (NSImageCell *)[matrix cellAtRow:0 column:0];
[cell setImage:[NSImage imageNamed:@"image.jpg"]];

另外,请记住,您的矩阵仅大到足以显示一个单元格,并且当您向其中添加列和行时,您需要增加其大小。

The NSMatrix doesn't draw anything by default as it's a container for the NSCells in it. You can verify that your NSMatrix is correctly added to the view by setting it to draw it's background.

[matrix setBackgroundColor:[NSColor blueColor]];
[matrix setDrawsBackground:YES];

Since the NSMatrix is a container for NSCell you need to fill them with something. In the example you posted you can do this by fetching the cell corresponding to your only row and column and setting the image.

NSImageCell *cell = (NSImageCell *)[matrix cellAtRow:0 column:0];
[cell setImage:[NSImage imageNamed:@"image.jpg"]];

Also, keep in mind that your matrix is only big enough to show one cell and as you add columns and rows to it you need to increase its size.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文