在特定的 NSTableview 单元格中绘制对角线

发布于 2024-11-18 15:50:09 字数 96 浏览 5 评论 0原文

有没有办法在 NSTableview 单元格中对角线绘制线条。您可以发布示例来执行此操作吗?我是 Mac 开发新手。请帮助我解决这个问题。

提前致谢.......

Is there a way to draw lines diagonally in NSTableview cell.Can u please post sample to do this.I am new to the Mac development.Please help me in this issue.

Thanks in advance.......

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

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

发布评论

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

评论(2

浊酒尽余欢 2024-11-25 15:50:09

是的,很容易。

您需要创建 NSTextFieldCell子类,它实际上是 NSTableView 用于显示文本的单元格类型。

对一个类进行子类化会创建该类的新版本,该版本可以完成原始类所做的所有工作以及更多功能。

这是使用 Xcode 4。如果您使用的是 Xcode 3,请告诉我。

在 Xcode 中,通过选择 File > 创建一个新文件。新的>新建文件...

新建文件

在弹出的工作表中选择< em>Objective-C 类并点击下一步

Objc class

使其成为 NSTextFieldCell 的子类,这就是我们将要制作修改副本的内容的。点击下一步

subclass

您可以将其另存为您想要的任何内容,但出于本教程的目的,请将其另存为 MyDiagonalLinedTextFieldCell< /em>.点击保存

 saving

应弹出两个新文件。

files

单击 .m 文件。这是说明类中的方法的用途的实现文件。
其内容应类似于以下内容:

//
//  MyDiagonalLinedTextFieldCell.m
//  CustomCell
//
//  Created by spudwaffle on 7/4/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "MyDiagonalLinedTextFieldCell.h"

@implementation MyDiagonalLinedTextFieldCell

- (id)init
{
    self = [super init];
    if (self) {
        // Initialization code here.
    }

    return self;
}

@end

init 方法下方添加一个 drawInteriorWithFrame: inView: 方法。
每次单元格需要在屏幕上渲染时,应用程序都会调用 drawInteriorWithFrame: inView: 方法。

您的代码现在应该如下所示:

@implementation MyDiagonalLinedTextFieldCell

- (id)init
{
    self = [super init];
    if (self) {
        // Initialization code here.
    }

    return self;
}

- (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {

}

@end

您需要做的第一件事就是绘制一个标准的 NSTextFieldCell
这可以通过调用来完成:

[super drawInteriorWithFrame:cellFrame inView:controlView];

这将在程序想要的确切区域中绘制一个普通的NSTextFieldCell

现在,我们需要绘制自定义线条。让我们将它们分开 5 像素,并将它们设置为 1 像素宽。
这需要一个 for 循环!

for (int i = 0; i < cellFrame.size.width/5; i ++) {

}

这使得 int 等于 0,每次循环运行时都会添加到该计数,并在 i 达到需要的行数时停止被绘制。

接下来,输入绘图代码来绘制线条。

for (int i = 0; i < cellFrame.size.width/5; i ++) {
        NSBezierPath *path = [NSBezierPath bezierPath];
        [path moveToPoint:NSMakePoint(i * 5, cellFrame.origin.y)];
        [path lineToPoint:NSMakePoint((i * 5) + 2, cellFrame.origin.y + cellFrame.size.height)];
        [[NSColor grayColor]set];
        [path setLineWidth:1];
        [path stroke];
}

这:

  1. 创建一个NSBezierPath,用于绘制线条和形状。
  2. 将路径的起点移动到单元格的底部边缘。
  3. 绘制一条线到单元格的顶部边缘。
  4. 将绘图颜色设置为灰色。
  5. 将绘图线宽设置为1
  6. 画线。

由于 for 循环,它对每一行一遍又一遍地执行此操作。

这是已完成的 MyDiagonalLinedTextFieldCell.m 文件。您现在无需担心 .h

#import "MyDiagonalLinedTextFieldCell.h"

@implementation MyDiagonalLinedTextFieldCell

- (id)init
{
    self = [super init];
    if (self) {
        // Initialization code here.
    }

    return self;
}

- (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
    [super drawInteriorWithFrame:cellFrame inView:controlView];
    for (int i = 0; i < cellFrame.size.width/5; i ++) {
        NSBezierPath *path = [NSBezierPath bezierPath];
        [path moveToPoint:NSMakePoint(i * 5, cellFrame.origin.y)];
        [path lineToPoint:NSMakePoint((i * 5) + 2, cellFrame.origin.y + cellFrame.size.height)];
        [[NSColor grayColor]set];
        [path setLineWidth:1];
        [path stroke];
    }
}

@end

现在,我们需要设置表视图中的单元格以使用此类。

单击您的 MainMenu.xib 文件。
单击表格视图的一行中的单元格,直到其变成蓝色。

blue cell

然后,点击右侧栏中的按钮,如下所示:
button

更改为MyDiagonalLinedTextFieldCell 并按 Enter 键。

class change

现在就开始吧,享受你的劳动成果吧!

custom cell window

修改绘图代码,直到获得所需的线条类型。

如有任何疑问,请随时与我联系。

Yes, easily.

You need to create a subclass of NSTextFieldCell which is actually the type of cell a NSTableView uses to display text.

Subclassing an class creates a new version of that class that does all that the original class did plus more.

This is using Xcode 4. If you are using Xcode 3 let me know.

In Xcode, create a new file by choosing File > New > New File...

New File

In the sheet that pops up choose Objective-C Class and hit Next.

Objc class

Make it a subclass of NSTextFieldCell, which is what we will be making a modified copy of. Hit Next.

subclass

You can save it as anything you want, but for the purposes of this tutorial, save it as MyDiagonalLinedTextFieldCell. Hit Save.

saving

Two new files should pop up.

files

Click on the .m file. This is the implementation file that tells what the methods in the class do.
Its contents should be similar to below:

//
//  MyDiagonalLinedTextFieldCell.m
//  CustomCell
//
//  Created by spudwaffle on 7/4/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "MyDiagonalLinedTextFieldCell.h"

@implementation MyDiagonalLinedTextFieldCell

- (id)init
{
    self = [super init];
    if (self) {
        // Initialization code here.
    }

    return self;
}

@end

Below the init method add a drawInteriorWithFrame: inView: method.
The application calls the drawInteriorWithFrame: inView: method each time the cell needs to render on screen.

Your code should now look like this:

@implementation MyDiagonalLinedTextFieldCell

- (id)init
{
    self = [super init];
    if (self) {
        // Initialization code here.
    }

    return self;
}

- (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {

}

@end

The first thing you need to do is just draw a standard NSTextFieldCell.
This can be done by calling:

[super drawInteriorWithFrame:cellFrame inView:controlView];

This draws a normal NSTextFieldCell in the exact area the program wants it to.

Now, we need to draw our custom lines. Let's put them 5 pixels apart and make them 1 pixel wide.
This calls for a for loop!

for (int i = 0; i < cellFrame.size.width/5; i ++) {

}

This makes a int that equals 0,adds to that count every time the loop runs, and stops when i reaches the amount of lines that need to be drawn.

Next, put in the drawing code to draw the lines.

for (int i = 0; i < cellFrame.size.width/5; i ++) {
        NSBezierPath *path = [NSBezierPath bezierPath];
        [path moveToPoint:NSMakePoint(i * 5, cellFrame.origin.y)];
        [path lineToPoint:NSMakePoint((i * 5) + 2, cellFrame.origin.y + cellFrame.size.height)];
        [[NSColor grayColor]set];
        [path setLineWidth:1];
        [path stroke];
}

This:

  1. Creates an NSBezierPath, which is used to draw lines and shapes.
  2. Moves the start of the path to the bottom edge of the cell.
  3. Draws a line to the top edge of the cell.
  4. Sets the drawing color to gray.
  5. Sets the drawing line width to 1.
  6. Draws the line.

It does this over and over for each line thanks to the for loop.

Here is the completed MyDiagonalLinedTextFieldCell.m file. You don't need to worry about the .h one for now.

#import "MyDiagonalLinedTextFieldCell.h"

@implementation MyDiagonalLinedTextFieldCell

- (id)init
{
    self = [super init];
    if (self) {
        // Initialization code here.
    }

    return self;
}

- (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
    [super drawInteriorWithFrame:cellFrame inView:controlView];
    for (int i = 0; i < cellFrame.size.width/5; i ++) {
        NSBezierPath *path = [NSBezierPath bezierPath];
        [path moveToPoint:NSMakePoint(i * 5, cellFrame.origin.y)];
        [path lineToPoint:NSMakePoint((i * 5) + 2, cellFrame.origin.y + cellFrame.size.height)];
        [[NSColor grayColor]set];
        [path setLineWidth:1];
        [path stroke];
    }
}

@end

Now, we need to set the cells in the table view to use this class.

Click on your MainMenu.xib file.
Click on the cell in a row of your table view until it turns blue.

blue cell

Then, hit the button in the right side bar that looks like so:
button

Change the Class to MyDiagonalLinedTextFieldCell and hit enter.

class change

Now hit run and enjoy the fruits of your labor!

custom cell window

Mess with the drawing code until you get the exact kind of lines you want.

Feel free to contact me with any questions.

心清如水 2024-11-25 15:50:09

这是一个漂亮的答案,而且表述得很好。不过,我还是尝试了一下,似乎不完整或不准确。我的 NSTableView 有 4 列,并将自定义单元格应用到右侧的单元格 - 由于某种原因,无论我做什么,只有第一(左)列会绘制特殊的对角线。

看来您的绘图代码中的逻辑缺少“对齐位置列”的某些步骤,我真的不知道该怎么做。

您还可以通过仅引入一次自定义单元 .m 并添加 .h 来改进示例,从而演示继承性。

This is a beautiful answer, and very well presented. Still, I tried it, and it seems to be incomplete or inaccurate. I have 4 columns in my NSTableView, and apply the custom cell to just the right one - for some reason only the FIRST (left) column gets the special diagonals drawn, no matter what I do.

It seems that the logic in your drawing code is missing some step of "aligning to the positional column" which I really don't know how to do.

you could also improve the sample by only introducing the custom cell .m once - and adding the .h to accompany it - thus demonstrating the inheritance.

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