Protocol

UITableViewDataSource

The methods adopted by the object you use to manage data and provide cells for a table view.

Declaration

@protocol UITableViewDataSource

Overview

Table views manage only the presentation of their data; they do not manage the data itself. To manage the data, you provide the table with a data source object—that is, an object that implements the UITableViewDataSource protocol. A data source object responds to data-related requests from the table. It also manages the table's data directly, or coordinates with other parts of your app to manage that data. Other responsibilities of the data source object include:

  • Reporting the number of sections and rows in the table.

  • Providing cells for each row of the table.

  • Providing titles for section headers and footers.

  • Configuring the table's index, if any.

  • Responding to user- or table-initiated updates that require changes to the underlying data.

Only two methods of this protocol are required, and they are shown in the following example code.

// Return the number of rows for the table.     
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
   return 0
}

// Provide a cell object for each row.
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
   // Fetch a cell of the appropriate type.
   let cell = tableView.dequeueReusableCell(withIdentifier: "cellTypeIdentifier", for: indexPath)
   
   // Configure the cell’s contents.
   cell.textLabel!.text = "Cell text"
       
   return cell
}

Use other methods of this protocol to enable specific features for your table. For example, you must implement the tableView:commitEditingStyle:forRowAtIndexPath: method to enable the swipe-to-delete feature for rows.

For information about how to create and configure your table's cells using your data source object, see Filling a Table with Data.

Specifying the Location of Rows and Sections

Table views communicate the location of cells to you using the row and section properties of NSIndexPath objects. Row and section indexes are zero based, so the first section is at index 0, the second at index 1, and so on. Similarly, the first row of each section is at index 0, which means you need both the section and row values to identify a row uniquely. If your table has no sections, you need only the row value.

Illustration that shows a table with multiple sections. The first section has an index of 0 and no row value. The nine rows in the first section have indexes between 0 and 8. The second section has an index of 1 and no row value. Its first row starts at index 0 again.

Topics

Providing the Number of Rows and Sections

- tableView:numberOfRowsInSection:

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

Required.

- numberOfSectionsInTableView:

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

Providing Cells, Headers, and Footers

- tableView:cellForRowAtIndexPath:

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

Required.

- tableView:titleForHeaderInSection:

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

- tableView:titleForFooterInSection:

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

Inserting or Deleting Table Rows

- tableView:commitEditingStyle:forRowAtIndexPath:

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

- tableView:canEditRowAtIndexPath:

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

Reordering Table Rows

- tableView:canMoveRowAtIndexPath:

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

- tableView:moveRowAtIndexPath:toIndexPath:

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

Configuring an Index

- sectionIndexTitlesForTableView:

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

- tableView:sectionForSectionIndexTitle:atIndex:

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

Relationships

Inherits From

See Also

Data

Filling a Table with Data

Create and configure cells for your table dynamically using a data source object, or provide them statically from your storyboard.

UITableViewDataSourcePrefetching

A protocol that provides advance warning of the data requirements for a table view, allowing you to start potentially long-running data operations early.

UILocalizedIndexedCollation

An object that organizes, sorts, and localizes the data for a table view that has a section index.

UIDataSourceTranslating

An advanced interface for managing a data source object.

UIRefreshControl

A standard control that can initiate the refreshing of a scroll view’s contents.

Beta Software

This documentation contains preliminary information about an API or technology in development. This information is subject to change, and software implemented according to this documentation should be tested with final operating system software.

Learn more about using Apple's beta software