SWIFT Tutorial - Exploring SWIFT basic with Tableview - SELISE

SWIFT Tutorial – Exploring SWIFT basic with Tableview

June 8, 2014

Following the tradition, at WWDC, 2014 Apple has brought an innovation, this time in programming language arena. This all new programming language sensation – Swift, is creating a buzz among all iOS developers and also the code freaks. As an iOS developer myself, I looked deeply into the ebook published by Apple and swift web reference guide and got astonished to see the syntax! Its surprisingly similar to C# (from the beginning of my programming life, I am in love with C#).

Here is my small list of stuff that I really love about Swift –

  • Its very similar to C# (did i say I love C# ?)
  • The fact that I don’t have to do  all the classical pointer (*) related stuff while dealing with “reference type”
  • With Swift we don’t need two files for any class. So like C# we have now one file basically to create a class.
  • Playgrounds is a great feature that allows developers to see how changes to their project affect their end product. Now we no need to compile every time to check the output.

Prerequisite for starting with Swift:

  1. Knowledge about ‘Objective-C’
  2. Idea about tableview
  3. Download XCode 6 – Beta

Lets dig into a swift tutorial with a very basic thing like “A table view” with some dummy data.

Step #1 : Creating a Project

Hit XCode-6 and create new XCode project. The screenshots below might come useful for you.

Create New XCode Project
Create New XCode Project
Select “Single View Application”
Select “Single View Application”
Fill all fields and select “Swift” as language
Fill all fields and select “Swift” as language

Select “Single View Application” (2nd image) and finally fill the 3rd image. It is important to select “Swift” as language. Finally select next and save it to somewhere.

Step #2: Creating a “table view”

Open up your storyboard and drag a “tableview” on it
Open up your storyboard and drag a “tableview” on it.

Create “IBOutlet” by control + dragging tableview from storyboard to class. Name it “mytable”. Okay, so now that the initial setup is done now we need to configure our class (ViewController) to work with “tableview”. It is much more similar to the way we did that on objective-C. Yes, We need to implement two protocol, (UITableViewDelegate & UITableViewDataSource)

implementing UITableViewDelegate & UITableViewDataSource
implementing UITableViewDelegate & UITableViewDataSource

You will see that, compiler is complaining at this point as because, “UITableViewDataSource” has a required method which we need to implement. Lets create a datasource and assign some values on it like below:

create a datasource and assign some values on

We have used an array of string and assigned some values on it on the “viewDidLoad” method. We also need to define tableview’s datasource and delegate to self and its okay as we have made it a subclass. Lets implement some methods to work on the tableview. If you see below, you can see that its very simple as we followed the exact same things for “earlier objective-C”


Now lets move to very basic things, tableview needs to know how many rows are there in “datasource”. It will get this information from below method:

func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {

return datas.count;


And, after knowing this, compiler will execute “functableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!” this method repeatedly (same as “datas.count” times).

var cell = UITableViewCell(style:UITableViewCellStyle.Subtitle, reuseIdentifier:"Cell");

var data = datas[indexPath.row];

if(data != nil){

cell.text = data;

if( cell.detailTextLabel != nil){

cell.detailTextLabel.text = data;



return cell;

One small note; you will see I have used semicolon(;) as I am used to this and it is optional here on “Swift”. It is required when you have two statement and you want these to be written in a single line.
What i did here is very simple, i have created a cell with subtitle style so that we can access “detailsTextLabel”.

Very important note: At any point if you get “Can’t unwrap ‘Optional.None
exception, don’t panic. It is because you are assigning something to a object which is nil. It is a good practice to use this kind of block (check whether this is not nil and then assign)

if( cell.detailTextLabel != nil){

cell.detailTextLabel.text = data;



Thats it: Run the project and you will see something like this:

Output - Tableview

Md. Arifuzzaman Md. Arifuzzaman is a Senior Software Engineer at SELISE rockin’ software. He has extensive experience in diverse facets of C#, .NET, BI development, and mobile app development (Objective-C, Android).