How to implement UITableView programmatically using Swift

When implementing a UITableView in your iOS application you will usually select a master-detail application:

Master Detail Application Swift

Once the project is selected the UITableView implementation is ready for you to start adapting in your application, but sometimes your need just a tableView.

The implementation of the tableView is very simple to do in Swift. Start by implementing the following functions:

This function is optional per documentation: “Default is 1 if not implemented”

Lets start the implementation of our tableView.

-Open Xcode and go to the file menu and select new and select new project or press Ctrl+Shift+N.
-Select the name of your project.
-Select the location where you want to save your project.

Single View Application Swift

Go to ViewController (ViewController.swift). In your ViewController you need to add the following delegates:


It will look like this:

Now you need to add the variables. Choose a variable where you are going to store the objects you are going to display in your tableView:

Now you need to initialize and set your tableView variable:

Because you are using only one section you are not going to implement the following function:

Now you need to get the count of elements you are going to display in your tableView:

Now you need to assign the values in your array variable to a cell:

Now you need to register when user taps a cell:

Your implementation should look like this:

If you click on the Run in your Xcode project should look like this:

UITableView Xcode Swift

Please let me know if you have any questions by leaving a comment below or on twitter @luisemedr

Leave a Reply

Your email address will not be published. Required fields are marked *