Table view is the view you should use when wanting to display a list of items.
The basic things regarding a table view are:
- It is consisted of sections. Each section contains rows.
- It is assembled by table view cells. Each cell is placed in a row.
- It is consisted of one column, and therefore it only allows vertical scrolling.
Before setting your tableview, you should implement its table view cell. Let's say for example that you have a list of names ["Anna", "Maria", "Hannah", "Emily"] that would like to display in the screen. Having already implemented a table view cell, with background colour yellow and a name string property, we will implement the table view. To implement a table view the steps are:
- Add the table view via Design screen.
- Go to Code > Protocols > TableViewDatasourceProtocol and add the required functions.
- The tableView:numberOfRowsInSection should return the number of "cells", therefore the number of objects that the user would like to list in the section with the given section index.
In this example since there are four names, we will return the number 4.
However, since we have a list, we can use the count of objects in the names list. If the table view has more than one section, you can use the section function argument to differentiate the number of rows in each section
- The tableView:cellForRow:andSection: should initialise a table view cell, set its properties and then return it.
In this case we set the table view cell's name property to the counterpart object in the names list. The row function argument informs in which row the certain cell will be placed and the section function argument in which section.
After having implemented these 4 steps you should be able to have a basic table view.