Creating Table Views
Table views consist of many different views and objects and, as a result, they are typically constructed within Interface Builder. Creating table views for both view-based and cell-based table views is identical. It is only when providing cells to table views that the techniques vary. See “View-Based Table Views” and “Populating Cell-Based Table Views” for more information.
Creating a Table View in Interface Builder
Table views are best created in Interface Builder. They consist of a number of individual classes that are combined in a form that makes programmatic creation far more complicated than many data views.
Open the nib file that you will be adding the table view to by selecting it in the project navigator so that it appears in the Interface Builder editor.
Drag an
NSTableViewobject from the Object library to the appropriate target window in the Interface Builder editor.Position and configure the the table view within the window.
Set the resizing behavior with respect to the window and any other related objects using the Size inspector.
Add and remove columns, as required, in the Attributes inspector. Remember that the table view itself must be selected for this step.
Connect an
IBOutletin the appropriate class to the table view. That allows for manipulating the table view and its contents.Set the table view’s delegate and datasource to the appropriate objects.
Setting the delegate and datasource objects for a table view can be done programmatically using
setDelegate:andsetDataSource:, or directly in interface builder by setting the IBOutlets of the delegate and datasource objects.
Adding Cells to View-Based Tables
While the cells for a view-based table can be made manually, it is far easier to create them in Interface Builder. The following steps provide the basics for creating view-based cells within a table.
Follow the steps in “Creating a Table View in Interface Builder” to add the table view to your user interface.
Drag an
NSTableCellViewobject or a “Custom View” from the Object Library to the appropriate row within the table view.There are two
NSTableCellViewtypes available in the Object library: “Image & Text Table Cell View” and “Text Table Cell View”. Choose the appropriate type for the cell when dragging the item from the Object library.If you choose to use a “Custom View”, set its view class in the Attributes inspector. Typically this will be a subclass of
NSTableCellView.Repeat the above step as often as necessary to provide cells for every column.
The table columns and the corresponding cells are identified by the User Interface item Identifier which is set in the Identity inspector.
The identifier can either automatically assign an identifier (and will ensure that the cell has the same identifier, and that the identifiers are in always in sync) or you can specify your own identifier for the table column that is more descriptive of the table column. See “Using Column and Cell Identifiers Effectively” for more information.
Adding Cells to Cell-Based Tables
Cells for a cell-based table can also be made manually, it is far easier to create them in Interface Builder. The following steps provide the basics for creating cells within a cell-based table.
Follow the steps in “Creating a Table View in Interface Builder” to add the table view to your user interface.
Drag the correct cell type from the Object Library to the appropriate column within the table view.
Only subclasses of
NSCellcan be inserted into cell-based table views..Repeat the above step as often as necessary to provide cells for every column.
Consider setting an identifier for each column. The identifier is the User Interface item Identifier which is set in the Identity inspector.
Setting a column identifier makes populating and retrieving columns substantially easier.
© 2011 Apple Inc. All Rights Reserved. (Last updated: 2011-07-06)