|
First version 09 Dec 2003
Last update 24 Mar 2004
Joar Wingfors, cocoa@joar.com
Cocoa tips, tricks and code snippets
The Subview-TableView
Table views are great, as long as the data you provide can be displayed by a cell. The table view displays text using a text cell, images using an image cell, and so on. You can use more elaborate cells, such as a button cell, or even a pop-up button cell, with a minimum amount of effort. However, there are a few controls in Cocoa that do not draw using cells, most notably NSProgressIndicator and NSColorWell. How would you create a table view using these controls? Apparently it can be done, as most web browsers use such a table view for their download windows.

Rule editors, like the one used in Mail, or the one used in the Find panel in Finder, are also difficult or impossible to create in the standard table view, since they require a layout that is not bound by columns of similar width, or even the same number of columns.


If the table view is so obviously not designed for these purposes, why try to use it at all? A valid question! My reason for sticking with the table view is that it provides so much functionality that would have to be duplicated if a custom class were constructed for this purpose.
I'm sure that there are lots of ways you could resolve this problem. I will present you with one solution, which I currently use in my production code. The implementation presented here is slightly stripped down, to make the code easier to read.
This implementation consists of only two classes, and very little code (always good). The core class is the SubviewTableViewController (STVC), which allows you to provide custom views to a standard NSTableView. The STVC sits between your controller and the designated table view. The STVC forwards all regular data source and delegate methods to your controller, and adds a couple of methods of it's own.
The sample projects are using the Xcode project file format, and a couple of Panther-only features. The core code of the STVC is not using anything Panther-specific though, and these projects could very easily be made to run on any version of Mac OS X.
SubviewTableViewTester
Try this example first. This simple application presents a table view with progress bars, similar to a browser's download panel.

Download: SubviewTableViewTester (Xcode 1.0 format)
SubviewTableViewRuleEditor
The following example is a bare bones rule editor, intended to showcase a slightly more advanced example on how to use STVC.

Download: SubviewTableViewRuleEditor (Xcode 1.0 format)
Document Version History
- 09 Dec 2003 - First post.
- 11 Dec 2003 - Improved some areas based on feedback.
- 24 Mar 2004 - Corrected small bug in SubviewTableViewController.m.
|