In this Swift Tutorials : iOS PROGRAMMING WITH SWIFT TUTORIALS AND LESSONS lesson we will tell you how to create Tableview from Scratch using Swift and Xcode (We made it in Xcode 8 using Swift

Follow Along

Open Xcode > New Project and select Single View Application

In the Product Name Type : TableView

Click “Main StoryBoard” in the Navigator

Find TableView in the objects library and drag it to viewController

Tableview 1

Go to pin, uncheck “Constrain to Margins” and after that set all 4 constraints to 0 and click “Add 4 Constraints”

TableView

Select a TableView, Click Resolve AutoLayout Issues and click “Update Frames”

Drag a TableViewCell to TableView

Set the identifier of the cell to “myTableViewCell”

Test it on the simulator

Open ViewController.swift file and type

after

Ctrl Drag a tableView from the ViewController in your StoryBoard to your ViewController.swift file (You will create an outlet)

In your viewDidLoad() function type:

With these two lines you are telling that your ViewController will be a delegate and a dataSource of your tableView.

After that we should setup number of rows in section. Let’s say we want 5 rows. Let’s do it using required function:

After that let’s write another required function:

If we run it now, we will have the following result:

TableView2

Great!

What is next?

Now It would be great to fill in the tableView Rows with some data.

Let’s create an Array of Names, for Example [“Alex”,”Nick”,”Max”,”John”] and let’s try to show them instead of “Test” in our TableView Cell. In our ViewController.swift let’s write:

Let’s make changes in numberOfRowsInSection function, it should look like this:

Let’s make changes in our cellForRowAt indexpath function

Build and run and see the result. It should look like this now:

TableView3

 

 

This lesson is a part of iOS PROGRAMMING WITH SWIFT TUTORIALS AND LESSONS created by the Apps-Top.com Team. Feel free to save and share this post to social if you find this lesson useful or helpful.