How to use UITableView in Swift

In this tutorial we will see how to use the UITableView in iOS

First you create single view application using XCODE
Then you place the UITableView control from the Object Library in to ViewController in your story board

To work with UITableView you need two protocols UITableViewDelegate and UITableViewDataSource

In above code we have days array to hold data to pass to the Tabelview. You can see the IBOutlet for the UITableview

With UITableViewDataSource protocol you have to conform two methods

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

Tells the data source to return the number of rows in a given section of a table view.
Now You can see the full source code for above two methods

First methods returns number of rows in the array
Second method return UITableViewCell object with assigning array element to it

Final thing you have to do is register your UITableViewCell and give them an identifier in viewDidLoad() function

Now lets look at the full source code of the ViewController.swift

You can have following output when you run the application

Touch your table row
If you want to popup alert when you touch the table row you can use the following code


I have experienced occasionally that UITableView not visible with data which we put in array. In those situations you can use following code inside the viewDidLoad() method