在特定的 NSTableview 单元格中绘制对角线
有没有办法在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,很容易。
您需要创建
NSTextFieldCell
的子类,它实际上是NSTableView
用于显示文本的单元格类型。对一个类进行子类化会创建该类的新版本,该版本可以完成原始类所做的所有工作以及更多功能。
这是使用 Xcode 4。如果您使用的是 Xcode 3,请告诉我。
在 Xcode 中,通过选择 File > 创建一个新文件。新的>新建文件...
在弹出的工作表中选择< em>Objective-C 类并点击下一步。
使其成为
NSTextFieldCell
的子类,这就是我们将要制作修改副本的内容的。点击下一步。您可以将其另存为您想要的任何内容,但出于本教程的目的,请将其另存为 MyDiagonalLinedTextFieldCell< /em>.点击保存。
应弹出两个新文件。
单击 .m 文件。这是说明类中的方法的用途的实现文件。
其内容应类似于以下内容:
在
init
方法下方添加一个drawInteriorWithFrame: inView:
方法。每次单元格需要在屏幕上渲染时,应用程序都会调用
drawInteriorWithFrame: inView:
方法。您的代码现在应该如下所示:
您需要做的第一件事就是绘制一个标准的
NSTextFieldCell
。这可以通过调用来完成:
这将在程序想要的确切区域中绘制一个普通的
NSTextFieldCell
。现在,我们需要绘制自定义线条。让我们将它们分开 5 像素,并将它们设置为 1 像素宽。
这需要一个
for
循环!这使得
int
等于0
,每次循环运行时都会添加到该计数,并在i
达到需要的行数时停止被绘制。接下来,输入绘图代码来绘制线条。
这:
NSBezierPath
,用于绘制线条和形状。1
。由于
for
循环,它对每一行一遍又一遍地执行此操作。这是已完成的
MyDiagonalLinedTextFieldCell.m
文件。您现在无需担心.h
。现在,我们需要设置表视图中的单元格以使用此类。
单击您的
MainMenu.xib
文件。单击表格视图的一行中的单元格,直到其变成蓝色。
然后,点击右侧栏中的按钮,如下所示:
将类更改为
MyDiagonalLinedTextFieldCell
并按 Enter 键。现在就开始吧,享受你的劳动成果吧!
修改绘图代码,直到获得所需的线条类型。
如有任何疑问,请随时与我联系。
Yes, easily.
You need to create a subclass of
NSTextFieldCell
which is actually the type of cell aNSTableView
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...
In the sheet that pops up choose Objective-C Class and hit Next.
Make it a subclass of
NSTextFieldCell
, which is what we will be making a modified copy of. Hit Next.You can save it as anything you want, but for the purposes of this tutorial, save it as MyDiagonalLinedTextFieldCell. Hit Save.
Two new files should pop up.
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:
Below the
init
method add adrawInteriorWithFrame: 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:
The first thing you need to do is just draw a standard
NSTextFieldCell
.This can be done by calling:
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!This makes a
int
that equals0
,adds to that count every time the loop runs, and stops wheni
reaches the amount of lines that need to be drawn.Next, put in the drawing code to draw the lines.
This:
NSBezierPath
, which is used to draw lines and shapes.1
.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.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.
Then, hit the button in the right side bar that looks like so:
Change the Class to
MyDiagonalLinedTextFieldCell
and hit enter.Now hit run and enjoy the fruits of your labor!
Mess with the drawing code until you get the exact kind of lines you want.
Feel free to contact me with any questions.
这是一个漂亮的答案,而且表述得很好。不过,我还是尝试了一下,似乎不完整或不准确。我的 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.