All things iOS & Swift

Working with UITableViewController [Swift]

I am writing a simple tutorial about UITableViewController using swift and XCode 6.

Download : Project File

Table view is a UI element in iOS that is mainly use to display data, navigation menu, or any other creative ideas you can make use of it.

Let us start! First thing to do is launch Xcode, create a new project, and select Single View Application.


Fill the text fields, refer below.


Click next and save the project, your screen should look like this.


Next, We are going to start designing in the storyboard.


Drag and drop Table View from the Object Library to the Storyboard


Run your project(cmd+r), simulator will open up as shown below.


Close the simulator and add table view cell from object library to the storyboard


In the attributes inspector, fill the identifier field and type TestCell so we can have a reference with table view cell.


Click the Assistant Editor  like shown below.


Click the Table View, ctrl+drag it to the viewController code view


Insert testTableView inside name textfield and click connect to create the outlet.

 outlet view IB

Now we’re going connect dataSource and delegate, press and hold ctrl key and click the table view then drag it to your view controller like shown below, repeat it twice with both dataSource and delegate connected.


I think that’s all the design and configuration we needed, now let us start coding.

Add, UITableViewDelegate and UITableViewDataSource protocol like shown below.


After that, we have to define both cellForRowAtIndexPath and numberOfRowsInSection method


Now, we create a dummy data, create an array called names like this, under the protocol declarations


Populate the array names


inside the scope of numberOfRowsInSection, type return names.count, it will return an Int data type and we’re going to use the count function for it to count how many values are there inside the array.


Type the following code below, first line of code inside the function, cellForRowAtIndexPath is the table cell identifier, remember the identifier field when we we’re designing? the value we entered inside identifier field, that value should be the same inside the testTableIdentifier string we are creating. The next line is creating a UITableViewCell called cell, assign this value, tableView.dequeueReusableCellWithIdentifier(testTableIdentifier) as UITableViewCell, “as” it means casting the value to UITableViewCell. Third line which is a conditional statement, it’s not really required, you can skip it if you want to, it’s for setting how your cell would look like, styling it. and finally both of the lines after the condition, cell.textLabel?.text is asking for the value, values(array) going to be feeded in the table, and return cell. 


Press and hold ctrl and press r, to run your simulator and you should see your array values feeded to the table like this.


Comment below if I miss anything or a topic to cover for new tutorials.

1 Discussion on “Working with UITableViewController [Swift]”

Leave A Comment

Your email address will not be published.