iOS UI TableView

Here we will learn iOS UI table view in swift with example and how to use iOS table view to show the data in table format with an example using Xcode editor.

IOS UI TableView

In iOS table view is used to show the data in a scrollable, single-column list of multiple rows that can be divided into sections. By using the iOS table view we can easily show the data in a list format and it providers simple yet efficient interface to interact with collections of data. Generally, iOS table view format will be like as shown below

 

ios ui tableview sample format

 

We can use Table View in our iOS applications by adding UITableView class reference.

 

Now we will see how to use the iOS UI Table view in our iOS applications with example.

Create iOS Table View App in Swift

To create new project in iOS Xcode open Xcode from /Applications folder directory. Once we open Xcode the welcome window will open like as shown below. In welcome window click on the second option “Create a new Xcode Project” or choose File à New à Project

 

Xcode application to create ios project

 

After selecting “Create a new Xcode project” a new window will open in that we need to choose a template.

 

The new Xcode window will contain several built-in app templates to implement the common type of iOS apps like page-based apps, tab-based apps, games, table-view apps, etc. These templates are having a pre-configured interface and source code files. 

 

For this iOS Table View example, we will use most basic template “Single View Application”. To select this one, Go to the iOS section on the left side à select Application à In the main area of dialog select “Single View Application” and then click on the next button like as shown below.

 

Select single view application from ios xcode templates

 

After click Next we will get window like as shown below, in this we need to mention project name and other details for our application.

 

Product Name: “TableView”

 

The name whatever we enter in the Product Name section will be used for the project and app.

 

Organization Name: “Tutlane”

 

You can enter the name of your organization or your own name or you can leave it as blank.

 

Organization Identifier: “com.developersociety”

 

Enter your organization identifier in case if you don't have any organization identifier enter com.example.

 

Bundle Identifier: This value will generate automatically based on the values we entered in Product Name and Organization Identifier.

 

Language: “Swift”

 

Select language type as “Swift” because we are going to develop applications using swift.

 

Devices: “Universal”

 

Choose Devices options as Universal it means that one application is for all Apple devices in case if  you have any specific requirement to run app only for iPad then you can choose the iPad option to make your application restricted to run only on iPad devices.

 

Use Core Data: Unselected

 

This option is used for database operations. In case if you have any database related operations in your application select this option otherwise unselect the option.

 

Include Unit Tests: Unselected

 

In case if you need unit tests for your application then select this option otherwise unselect it.

 

Include UI Tests: Unselected

 

In case if you need UI tests for your application then select this option otherwise unselect it.

 

Once you finished entering all the options then click on the Next button like as shown below.

 

ios create new tableview project in xcode

 

Once we click on the Next button new dialog will open in that we need to select the location to save our project. Once you select the location to save the project then click on Create button like as shown below

 

Give path to save new ios application in xcode

 

After click on the Create button, the Xcode will create and open a new project. In our project Main.storyboard and ViewController.swift are the main files that we used to design the app user interface and to maintain source code. 

 

Main.storyboard - Its visual interface editor and we will use this file to design our app user interface 

 

ViewController.swift - It contains source code of our application and we use this file to write any code related to our app.

 

Now in project select Main.storyboard file the Xcode will open visual interface editor like as shown below.

 

ios tableview project storyboard file in xcode

 

Now select ViewController.swift file in your project that view will be like as shown below.

 

iOS tableview project viewcontroller file in xcode

 Add iOS UI Controls to View in Swift

Now we will add controls to our application for that open Object Library. The Object Library will appear at the bottom of Xcode in right side. In case if you don't find Object library, click on the button which is at the third position from the left in the library selector bar like as shown below. (Alternatively you can choose View à Utilities à Show Object Library.)

 

Object Library in Xcode Application to Search for UI Controls

 

As we discussed our user interface will be in Main.storyboard file so open Main.storyboard file. Now in Object library search for the Table View in Filter field then drag and drop the Table View into Main.storyboard ViewController like as shown below.

 

ios tableview add ui controls to storyboard in xcode

 

Now click on table view and you see that in Prototype cells the textbox is zero give it here “1” and style should be “Grouped” like as shown below

 

ios tableview change prototype cells in xcode

 

Now we will make connection between Table View and View Controller for that click on the yellow button and drag the cursor from the yellow button and drop it on data source and you should do the same process for connecting delegate also like as shown below

 

ios map tableview to datasource and delegate in xcode

 

For the implementation of Table View move to the ViewController.Swift file first adds the two classes which is UITABLEVIEWDATASOURCE and UITABLEVIEWDELEGATE and then make an array with name devCourses which is used to show the data in table view. let keyword is basically the constant keyword it mean that the value can’t be changed after declaration.

 

class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {

let devCourses = (

"iOS App Dev with Swift Essential Training","iOS 8 SDK New Features",

"Data Visualization with D3.js",

"Swift Essential Training",

"Up and Running with AngularJS",

"MySQL Essential Training",

"Building Adaptive Android Apps with Fragments",

"Advanced Unity 3D Game Programming",

"Up and Running with Ubuntu Desktop Linux”,

"Up and Running with C"

)

Now our task is to show the data in table view for that we have three methods which are mandatory to implement in Table View those are given below. The first method is “numberOfSectionsInTableView” by using this method we can return a number of sections which required in our applications. Present we need only one section so we are returning only 1.

 

func numberOfSectionsInTableView(tableView: UITableView) -> Int{

return1

}

Now second method is “tableView” here we need to define number of row sections which we need show in table view. 

 

In case If we want to show only five rows in table view then we need to mention it as “return 5” otherwise if we want to show all the rows present in an array then we need to mention the array name with count method like as mentioned below.

 

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

returndevCourses.count

}

Now the third method is “tableView” with “cellForRowAtIndexPath” basically this is the method where we assign array data to label in our Table View cells. Our method need to be like as shown below

 

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

let cell=tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell

let courseTitle = devCourses[indexPath .row]

cell.textLabel?.text=courseTitle

return cell

}

Once we finished writing all the required functionality in ViewController.swift file our code will be like as shown below

 

//

//  ViewController.swift

//  TableView

//

import UIKit

class ViewController: UIViewController,UITableViewDataSource {

let devCourses = (

"iOS App Dev with Swift Essential Training","iOS 8 SDK New Features",

"Data Visualization with D3.js",

"Swift Essential Training",

"Up and Running with AngularJS",

"MySQL Essential Training",

"Building Adaptive Android Apps with Fragments",

"Advanced Unity 3D Game Programming",

"Up and Running with Ubuntu Desktop Linux”,

"Up and Running with C"

)

func numberOfSectionsInTableView(tableView: UITableView) -> Int{

return1

}

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

return devCourses.count

}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

let cell=tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell

let courseTitle = devCourses[indexPath .row]

cell.textLabel?.text=courseTitle

return cell

}

override func viewDidLoad() {

super.viewDidLoad()

}

override func didReceiveMemoryWarning() {

super.didReceiveMemoryWarning()

// Dispose of any resources that can be recreated.

}

}

Now we will run and check the output of application. To run the application, select the required simulator (Here we selected iPhone 6s Plus) and click on the Play button, located at the top-left corner of the Xcode toolbar like as shown below.

 

Run ios tableview example in xcode using swimulator

Output of iOS Table View App in Swift

Following is the result of the iOS Table View application. If you observe we are showing data as a single-column list with multiple row sections.

 

ios tableview example result in swift xcode

 

This is how we can use the ios UI control table view in swift applications to show data as a single column list of multiple rows based on our requirement.