Shaideru

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.

1

Fill the text fields, refer below.

2

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

3

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

4

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

5

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

6

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

6-2

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

6-3

Click the Assistant Editor  like shown below.

inspector

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

6-1

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.

78

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

Add, UITableViewDelegate and UITableViewDataSource protocol like shown below.

9

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

 methods

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

10

Populate the array names

11

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.

12

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. 

13

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

14

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.