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…….
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Yes, easily.
You need to create a subclass of
NSTextFieldCellwhich is actually the type of cell aNSTableViewuses 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
initmethod 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
NSTextFieldCellin 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
forloop!This makes a
intthat equals0,adds to that count every time the loop runs, and stops whenireaches 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
forloop.Here is the completed
MyDiagonalLinedTextFieldCell.mfile. You don’t need to worry about the.hone for now.Now, we need to set the cells in the table view to use this class.
Click on your
MainMenu.xibfile.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
MyDiagonalLinedTextFieldCelland 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.