As discussed in the previous chapter, we need to add a data source and delegate to our Table View. Table View uses dynamic cells, which we are required to add:
- Select Table View in the Outline view, and then Connections inspector in the Utilities panel.
- Click on and drag from dataSource to the Location View Controller in the Outline view:

- Repeat with the delegate property:

- In the Utilities panel, select the Attributes inspector, if not already selected, and make sure you have the following values:
-
- Style: Basic
- Identifier: locationCell
- Selection: Gray
- Accessory: Disclosure indicator
Next, for us to display anything in Tableview, we need to add the UITableViewDataSource protocol. Our protocol requires that we implement the following three methods. Add the following after the closing curly brace of viewDidLoad():

Let's break down the code to understand what we are doing:
- Part A: This method tells our Table View how many rows we want to display.
tableView(_:numberOfRowsInSection:)
- Part B: Here, we tell our Table View that we want to display 15 rows.
return 15
- Part C: This method tells our Table View how many sections we want to display. Sections in Table Views are typically used as headers, but they can be used however you choose.
numberOfSections(in:)
- Part D: We tell our Table View that we only want one section.
return 1
- Part E: Our third and final method gets called for every item we need. Therefore, in our case, it gets called 15 times.
tableView(_:cellForRowAt:)
- Part F: Here, we create a cell every time Part E is called, either by taking one from the queue, if available, or by creating a new cell. The identifier, locationCell, is the name we gave it in the storyboard. Therefore, we are telling our Table View that we want to use this cell. If we had multiple Table Views, we would reference the identifier for the row and section in which we want the cell to display.
let cell = tableView.dequeueReusableCell(withIdentifier: "locationCell", for: indexPath) as UITableViewCell
cell.textLabel?.text = "A cell"
Since we do not have any data yet, we set our label to A cell. The textLabel variable is the default label we got when we selected a basic cell.
- Part G: Finally, after each time we create a new cell, we give the cell back to the Table View to display that cell.
return cell
Let's build and run the project by hitting the Play button (or using cmd + R) to see what happens. You should now see A cell repeating 15 times:
