UITableView L3.6.1

Table views have two delegate protocols with a total of around 45 methods.

The delegate is primarily used for responses to user events, so the table uses it's delegate protocol to ask event questions like the ones below. The data source is used for access to data and the cells, and to ask data related questions to the table's delegate.

tableviewdelegate and datasource

Common UITableView Method Signatures

  • tableView(_:canEditRowAt:)
  • tableView(_, cellForRowAt:)
  • sectionIndexTitles(for:)
  • tableView(_, numberOfRowsInSection)
  • tableView(_:heightForRowAt:)
  • tableView(_:didSelectRowAt:)
  • tableView(_:shouldIndentWhileEditingRowAt:)
  • textField(_:shouldChangeCharactersIn:replacementString:)

of these methods the only required ones are

  • tableView(_:didSelectRowAt:)
  • tableView(_, cellForRowAt:)
  • tableView(_, numberOfRowsInSection)

Cell Usage and Queues

Each cell is a fairly complex view object so Apple has worked out a cacheing scheme like stairs on an escalator.

cells in tableview

when cell objects scroll off the screen they get reused on the other side anything that is shared across cells gets reused (i.e. cell border, image placement, labels etc). Basically what this means is that cells don't need to be respecified when a particular cell comes on to the screen only the data needs to be respecified.

At any given time there will be a few table view cell objects that are off the screen in a queue waiting to come back on screen with new data. Because of this we never need to construct cells ourselves the queue is kept inside the table view so whenever we need a cell we dequeue one from table view and populate it with data.

Some table views contain more the one type of cell, to accommodate that cells are tagged with reuseIdentifier attributes to keep them seperate. In these cases table views keep different queue for each type of cell.

cells in tableview

Steps for populating table cells

cells in tableview

Setting up a table in storyboard

setting up a table

Connecting the dataSource to the view controller

connecting the dataSource

To define how the cell should display it's data it's important to set the prototype cell in the attributes inspector and not drag from the object library.

setting prototype cell

Cell Types

Custom custom cell

Basic basic cell

Right Detail right cell

Left Detail left cell

Subtitle subtitle cell