Protocol

UITableViewDataSource

The UITableViewDataSource protocol is adopted by an object that mediates the application’™s data model for a UITableView object. The data source provides the table-view object with the information it needs to construct and modify a table view.

Overview

As a representative of the data model, the data source supplies minimal information about the table view’s appearance. The table-view object’s delegate—an object adopting the UITableViewDelegate protocol—provides that information.

The required methods of the protocol provide the cells to be displayed by the table-view as well as inform the UITableView object about the number of sections and the number of rows in each section. The data source may implement optional methods to configure various aspects of the table view and to insert, delete, and reorder rows.

Many methods take NSIndexPath objects as parameters. UITableView declares a category on NSIndexPath that enables you to get the represented row index (row property) and section index (section property), and to construct an index path from a given row index and section index (init(row:section:) class method). (The first index in each index path identifies the section and the next identifies the row.)

Symbols

Configuring a Table View

func tableView(UITableView, cellForRowAt: IndexPath)
Required.

Asks the data source for a cell to insert in a particular location of the table view.

func numberOfSections(in: UITableView)

Asks the data source to return the number of sections in the table view.

func tableView(UITableView, numberOfRowsInSection: Int)
Required.

Tells the data source to return the number of rows in a given section of a table view.

func sectionIndexTitles(for: UITableView)

Asks the data source to return the titles for the sections for a table view.

func tableView(UITableView, sectionForSectionIndexTitle: String, at: Int)

Asks the data source to return the index of the section having the given title and section title index.

func tableView(UITableView, titleForHeaderInSection: Int)

Asks the data source for the title of the header of the specified section of the table view.

func tableView(UITableView, titleForFooterInSection: Int)

Asks the data source for the title of the footer of the specified section of the table view.

Inserting or Deleting Table Rows

func tableView(UITableView, commit: UITableViewCellEditingStyle, forRowAt: IndexPath)

Asks the data source to commit the insertion or deletion of a specified row in the receiver.

func tableView(UITableView, canEditRowAt: IndexPath)

Asks the data source to verify that the given row is editable.

Reordering Table Rows

func tableView(UITableView, canMoveRowAt: IndexPath)

Asks the data source whether a given row can be moved to another location in the table view.

func tableView(UITableView, moveRowAt: IndexPath, to: IndexPath)

Tells the data source to move a row at a specific location in the table view to another location.

Relationships

Inherits From