About Table Views in OS X Applications
A table view displays data for a set of related records, with rows representing individual records and columns representing the attributes of those records. For example, in a table of employee records, each row represents one employee, and the columns represent such attributes as the first and last name, address, salary, and so on. Table views are versatile user-interface elements that are found in many Mac apps.
A table view can have a single column or multiple columns and allows scrolling both vertically and horizontally. It consists of rows, each of which have a a corresponding cell that represents a field in a data collection.
In OS X version 10.6 and earlier each individual cell within a table view was required to be a subclass of
NSCell. This approach has caused limitations when designing complex custom cells, typically requiring the developer to write their own
NSCell subclass. Additionally, providing animation, such as progress views, was extremely difficult. Throughout this book these types of table views are referred to as cell-based table views.
In OS X version 10.7 table views have been redesigned and now support using views as individual cells. These are referred to as view-based table views. View-based table views allow you to design custom cells in the Interface Builder portion of Xcode 4.0. It allows easy design time layout as well as making it easy to animate changes and customize drawing. As with cell-based table views, view-based table views support selection, column dragging, and other user-expected table view behavior. The only difference is that the developer is given much more flexibility in design and implementation.
Creating view-based and cell-based table views and adding columns use the same techniques within Interface Builder. The differences occur in your application code when providing the individual cells, populating the content of the table view, and customizing the table view appearance. As well, the Cocoa bindings techniques are entirely different between the two implementations.
Throughout the book view-based table view and cell-based table view specific functions are called out in the chapter titles. Some chapters, for example row selection are the same when using both techniques. Chapters that are applicable to both technologies will be called out as such.
At a Glance
Understanding the structure of a table view, and knowing how to build one, lets you add a compelling user interface to Mac apps that work with collections of information.
Creating Table Views Using interface Builder
Interface Builder is the most common method of creating a table view. You create a table view in Interface Builder by dragging the table view prototype from the Library palette and positioning it within a window or superview. You then add table columns to the table view and arrange them as desired, possibly providing visible headers with names. Many aspects of table columns can be set directly in Interface Builder allowing you to avoid the need to write additional code.
Once the table is created, if you are using view-based table views, you drag instances of
NSTableCellView into the table columns and modify them to represent the interface required for the column.
Providing the Data to the Table View
You must provide data to the table view. This can be done using one of two methods: programmatically by implementing a datasource class or by establishing a relationship between the table view and a controller using Cocoa bindings.
When providing data programmatically you create a class that conforms to the
NSTableViewDataSource protocol and implement the method that provides the row and column data as requested. Content can simply be displayed and, when appropriate, can also be edited.
You can also provide content to a table view using Cocoa bindings. Bindings allows you to create a relationship between a controller class instance, which manages the interaction between data objects and the table view. The bindings approach allows you to bypass the datasource class for providing the data as well as supporting editing of the data.
Because the techniques for view-based cell creation and populating and cell-based table view populating and editing are quite different these chapters have been separated by topic.
Responding to Selections
When the user selects a row by clicking on it, the delegate of the table view is notified that the selection is changing. The delegate then has the opportunity to allow or deny the selection of the row.
The selection functionality is identical with both view-based and cell-based table views.
How to Use This Document
This document describes the capabilities of the table view. The view-based table views are dissected to highlight their individual view components, and additional chapters describe how to create table views in general, view-based table views, and how to populate both styles programmatically and using Cocoa bindings.
Additional table components are also discussed including sorting and row selection. Cell-based table views are also discussed in this document.
To develop successfully with the
NSTableView class, you need a strong grasp of the model-view-controller design pattern. Refer to “The Model-View-Controller Design Pattern” in Cocoa Fundamentals Guide.
NSTableView instances can be used with Cocoa bindings, both in view-based and cell-based modes. However it is sternly suggested that learning and understanding the programmatic interface of the table view be understood before beginning to use the more advanced Cocoa bindings. To understand bindings start with the Core Competency article on Cocoa bindings. For a complete understanding, refer to Cocoa Bindings Programming Topics and “Synchronize Your OS X Views and Data with Cocoa Bindings” in Xcode User Guide.
It is recommended that you read the OS X Human Interface Guidelines that provides information on the recommended uses of table views.
You will find the following sample-code projects to be instructive when designing your own table view implementations:
With and Without Bindings project suite
Cocoa Tips and Tricks project. Amongst other topics, it includes projects the demonstrate how to use hyperlinks within a tableview and how to implement variable row height sizes within table view rows.
TableViewPlayground project. This project uses view-based table views to demonstrate a wide range of capabilities including: populating cells programmatically and with Cocoa bindings, creating a simple
NSTableCellViewclass based table, using custom
NSTableRowViewinstances supporting the pasteboard which allows single cell dragging and multi-image dragging. It also demonstrates the new view-based capabilities that have been added to the
© 2011 Apple Inc. All Rights Reserved. (Last updated: 2011-07-06)