Recently I have become more and more interested with the wonderful world of Objective-C and C. For the last couple years I have been a hobbyist developer and still am today. I never really felt that my development skills had reached any level of real skill and so I have been an active member on websites like Stack overflow, Apple Developer Forums and more asking over and over the answers to what were probably very simple issues and questions that I hurdled over while learning.
I still do not feel that I am a very good developer but I figured that now was my chance to at least take some of the knowledge that I worked hard to learn and pass that on to help other people make great looking OSX Applications. I have created a Cocoa Control called iTunes-Table-Header. This control is a collection of three classes 2 of which are subclasses of NSTableView and NSArrayController.
The goal of this website is to help beginner to novice developers style their applications to the level of an advanced developer while teaching the fundamentals of how to automatically sort a column on wake. How to change the row selection color, and how to load sample data into an NSTableView, all things that took me years to learn. So lets begin!
The final outcome of the project will look like the following.
**The Boring stuff? **
The first step in the process is to subclass NSTableView to do so I am using the following code.
iTableStyle : Subclass of NSTableView
The above code does the following. It allows us to override the color of the selected cell and it draws the select color of the row. This tutorial and this control mimics the same color selection found in iTunes 11.1.2 for OSX Mavericks. The below row select color is the final result here.
The next step in the process is to create the subclass of NSTableHeaderCell. Below are the class files but you can not directly link these files in the XIB to this class as an object because we do not have any outlets but rather just an array. This is intentional below we will talk about the appropriate way to link these together for now here is the code.
iHeaderStyle.h : Subclass of NSTableHeaderCell
iHeaderStyle.m : Subclass of NSTableHeaderCell
So what do the above classes do? They are responsible for changing the look and feel of the NSTableHeader row, making sure the toggle disclosure triangle image is still drawn and adds column separators in the header.
We now have to link it all up this is where it gets a little tricky. You can not directly subclass this object in Xcode using the interface builder tool. You must link all the components with your controlling class object or what I did was to use my AppDelegate class object to reference all the parts of the interface properly.
as you can see in the screenshot below I have linked the Window and the Table View to the appropriate items in the XIB file.
Now that those connections are made look at the AppDelegate.m file and see how the above subclass script that we used above is now referenced in the AppDelegate or other object class that your using.
You see how we are calling iHeaderStyle.h in the AppDelegate.m file, this means that we can reference the outlets that are linked to that object inside of this class. So what does this all do. Well simply put it is what is responsible for telling the program that when it loads how to draw the NSTableView.
So thats it! Now your tableview looks like the iTunes one right? Nope! There are a few other things you must do. You must set the row alternating color you do that in Xcode like so.
Look carefully at the background color and the alternating row color. These colors match the colors in iTunes for Mavericks. Background Color: RGB Values are 255, 255, 255. Grid color RGB values are 215, 220, 228.
Now we are beginning to visually look more iTunes like. But wait we can’t actually test the toggle functionality until we get some data into our tables. For that we need to take our nice little set of code and turn it into a functional program. Hold onto your hats!!
Create a simple data application.
I am going to show you- how to perform bindings between NSTableColumn, NSArrayController and NSUserDefaultsController, so that tabular data can be stored and retrieved from NSUserDefaults without writing a single line of code
Select MainMenu.xib, arrange buttons and table view on window as shown in below screen shot. Also add array controller to the xib. In my case I have renamed array controller as Artist Array Controller, which will be visible in next few screen shots.
Perform Content Array binding for Array Controller, as shown:
Tip : Don’t forget to mark - ‘Handles Content As Compound Value’ as checked!
Perform Value binding for Artist - Table Column, as shown:
Bind ‘+’ button to add action of Array Controller.
Bind ‘-‘ button to remove action of Array Controller like you just did above. Finally bind ‘Save’ button to save: action of Shared User Default Controller.
Run the project
Now try selecting ‘+’ / ‘-‘ button, editing added rows, click ‘Save’ button then quit and re-launch the application to see the saved values.
You can check out my first contribution to the Cocoa Community here on Cocoa Controls and you can check out all my Sample Code here where you can check out the GitHub project that is dedicated to this control. I hope you all enjoyed!! This is MIT Licensed
We have assisted many businesses in implementing MDMs, developing custom security policies and procedures, and redesigning their networks.The list goes on and on. Contact us today and see how we can help you too.Contact Sales
We are a remote and fully distributed, Nationwide Apple focused MSP serving Washington DC, Philadelphia, New York, Chicago, San Francisco, San Diego & more.
We focus on providing top notch Mac Support for small to mid-sized businesses. Contact us, and learn how we can help your company.