Mac Developer Library

Developer

AppKit Framework Reference NSTableViewDataSource Protocol Reference

Options
Deployment Target:

On This Page
Language:

NSTableViewDataSource

The NSTableViewDataSource protocol declares the methods that an instance of NSTableView uses to provide the data to a table view and allow editing of the contents of its data source object.

Some of the methods in this protocol, such as tableView:objectValueForTableColumn:row: and numberOfRowsInTableView: along with other methods that return data, are called frequently, so they must be efficient.

If you’re not using Cocoa bindings to provide data to the table view, the following methods are required:

To learn more about Cocoa bindings, see Cocoa Bindings Programming Topics.

  • Returns the number of records managed for aTableView by the data source object.

    Declaration

    Swift

    optional func numberOfRowsInTableView(_ tableView: NSTableView) -> Int

    Objective-C

    - (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView

    Parameters

    aTableView

    The table view that sent the message.

    Return Value

    The number of rows in aTableView.

    Discussion

    An instance of NSTableView uses this method to determine how many rows it should create and display. Your numberOfRowsInTableView: implementation is called very frequently, so it must be efficient.

    Both view-based table views and cell-based table views must implement this method.

    Availability

    Available in OS X v10.0 and later.

    Available as part of an informal protocol prior to OS X v10.6.

  • Called by the table view to return the data object associated with the specified row and column.

    Declaration

    Swift

    optional func tableView(_ tableView: NSTableView, objectValueForTableColumn tableColumn: NSTableColumn?, row row: Int) -> AnyObject?

    Objective-C

    - (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex

    Parameters

    aTableView

    The table view that sent the message.

    aTableColumn

    A column in aTableView.

    rowIndex

    The row of the item in aTableColumn.

    Return Value

    An item in the data source in the specified table column of the view.

    Discussion

    tableView:objectValueForTableColumn:row: is called each time the table cell needs to be redisplayed, so it must be efficient.

    Availability

    Available in OS X v10.0 and later.

    Available as part of an informal protocol prior to OS X v10.6.

  • Sets the data object for an item in the specified row and column.

    Declaration

    Swift

    optional func tableView(_ tableView: NSTableView, setObjectValue object: AnyObject?, forTableColumn tableColumn: NSTableColumn?, row row: Int)

    Objective-C

    - (void)tableView:(NSTableView *)aTableView setObjectValue:(id)anObject forTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex

    Parameters

    aTableView

    The table view that sent the message.

    anObject

    The new value for the item.

    aTableColumn

    A column in aTableView.

    rowIndex

    The row of the item in aTableColumn.

    Discussion

    This method is intended for use with cell-based table views, it must not be used with view-based table views. In view-based tables, use target/action to set each item in the view cell.

    Availability

    Available in OS X v10.0 and later.

    Available as part of an informal protocol prior to OS X v10.6.

  • Called by aTableView to indicate that sorting may need to be done.

    Declaration

    Swift

    optional func tableView(_ tableView: NSTableView, sortDescriptorsDidChange oldDescriptors: [NSSortDescriptor])

    Objective-C

    - (void)tableView:(NSTableView *)aTableView sortDescriptorsDidChange:(NSArray<NSSortDescriptor *> *)oldDescriptors

    Parameters

    aTableView

    The table view that sent the message.

    oldDescriptors

    An array that contains the previous descriptors.

    Discussion

    The data source typically sorts and reloads the data, and adjusts the selections accordingly. If you need to know the current sort descriptors and the data source doesn’t manage them itself, you can get the current sort descriptors by sending aTableView a sortDescriptors message.

    Availability

    Available in OS X v10.3 and later.

    Available as part of an informal protocol prior to OS X v10.6.