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.

bullet
To create a table view in interface builder
  1. 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.

  2. Drag an NSTableView object from the Object library to the appropriate target window in the Interface Builder editor.

  3. Position and configure the the table view within the window.

  4. Set the resizing behavior with respect to the window and any other related objects using the Size inspector.

  5. Add and remove columns, as required, in the Attributes inspector. Remember that the table view itself must be selected for this step.

  6. Connect an IBOutlet in the appropriate class to the table view. That allows for manipulating the table view and its contents.

  7. 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: and setDataSource:, 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.

bullet
To add cells to a View-based table view
  1. Follow the steps in “Creating a Table View in Interface Builder” to add the table view to your user interface.

  2. Drag an NSTableCellView object or a “Custom View” from the Object Library to the appropriate row within the table view.

    There are two NSTableCellView types 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.

  3. Repeat the above step as often as necessary to provide cells for every column.

  4. 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.

bullet
To add cells to a cell-based table view
  1. Follow the steps in “Creating a Table View in Interface Builder” to add the table view to your user interface.

  2. Drag the correct cell type from the Object Library to the appropriate column within the table view.

    Only subclasses of NSCell can be inserted into cell-based table views..

  3. Repeat the above step as often as necessary to provide cells for every column.

  4. 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.


Did this document help you? Yes It's good, but... Not helpful...