A table is implemented by several classes, including NSTableView
, NSTableHeaderView
, NSTableColumn
, NSTableHeaderCell
, and possibly others. The primary class is NSTableView
.
A table view is usually displayed together with a corner view and header view inside a scroll view (NSScrollView
) object. The default corner view is a simple view object that fills in the corner above the vertical scroller. The header view is usually an instance of NSTableHeaderView
that draws the column headers and handles column selection, rearranging, and resizing. If a table view is enclosed by a scroll view, the table view is the document view of the scroll view object.
You can replace the default corner view and header view of a table with custom views if you want. For example, you might replace the corner view with a button that sorts the table contents using the selected column. To replace the views, you use the setCornerView:
and setHeaderView:
methods of NSTableView
. During window layout, the scroll view retrieves your custom views using the cornerView
and headerView
methods of NSTableView
and tiles them along with the scrollers and document view.
Because the NSTableView
and NSTableHeaderView
objects both need access to column information (such as the column width), this information is encapsulated in NSTableColumn
objects. An NSTableColumn
object stores the width of the column, an attribute identifier used by the data source to identify the column contents, and flags that indicate whether the user can resize the column or edit its cells. (For information about the attribute identifier of a column, see “Using a Table Data Source.”) The object also stores two NSCell
objects. The first cell object is used by the header view to draw the column header. The second cell object is used by the NSTableView
to draw the data values in the column; the table column object reuses the same NSCell
object for each row in the column.
The cell used to draw each column header is an instance of the NSTableHeaderCell
class by default. An NSTableHeaderCell
object contains the title displayed over the column as well as the font and color of that title. You use the API of its superclasses, NSTextFieldCell
and NSCell
, to set a column’s title and to specify display attributes for that title (font, alignment, and so on). You can also use the setImage:
method of NSCell
to display an image instead of a title. To remove the image and restore the title, use the setStringValue:
method of NSCell
.
The cell used to draw the column's data values is typically an instance of NSTextFieldCell
but can be an instance of any NSCell
subclass, such as NSImageCell
. This object is used to draw all values in the column and determines the font, alignment, text color, and other such display attributes for those values. You can customize the presentation of various kinds of values by assigning an NSFormatter
object to the cell. For example, to properly display date values in a column, you could assign an NSDateFormatter
object to the cell.
In Mac OS X version 10.3 and later, you can configure text in table cells to display an ellipsis glyph (…) when the column is too narrow to display the full text. To do this, you configure the table column cells with the message cell setWraps:YES
. Then, for the object value returned by the data source, use an attributed string configured with the desired paragraph style. For a thorough explanation of this technique, see “Breaking Lines by Truncation”.
Last updated: 2010-01-20