Table View Cells Directing to Multiple Views [For Beginners]

Today we are going to learn how to make app like table view cells directing to multiple views.

Basically the application will look like the below image and when we select one of the cell it will direct to another view which we want with a navigation controller.

Ok then lets start with configuring a new project and do coding

First start a new Xcode project,take single view application and Name it.Select the language as Swift and Devices as Universal.No need of core data and other stuff.Save it wherever you want and here we go..

First delete the initial view controller you get and drag and drop a Table View Controller by the Utility panel at the right bottom corner of the window.

Then we have to add new swift file for this new Table View Controller.For that go to File -> New -> File -> And elect Swift file.

Screen Shot 2016-04-21 at 3.34.42 AM

Name it as whatever you want and save it.This is where we gonna code all the things in this tutorial.

Start with making a new class file and i’ve named it as “TableViewController” .Before starting the code here make sure you link the Table View  Controller with this class file.

You will be needing some more View Controllers as you want with the number of cells you gonna put.So just drag and drop the VCs from the object panel and connect them by Storyboard IDs.

So all is set to go.Here you can find my Class code and i have commented some stuff to make this more clear and understandable.

import Foundation

import UIKit

//This class should link with the UI of table view

class TableViewController: UITableViewController {

    //names are the cells of the table view and used indentities for get the corresponding View Controllers
    var names = [String]()
    var identities = [String]()

    //Things that we want when the application loads
    override func viewDidLoad() {
        names = [Akila,Jayaranga,Wasala,UCSC]
        identities = [A,B,C,D]

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return names.count

    //This function passes the data which is is in the names array to the cells
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        //Make sure you put the identifier in the UI as "Cell"
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell")
        cell?.textLabel!.text = names[indexPath.row]
        return cell!

    //This is the function for change the view controllers after clicking on cells.This is where i used the identifiers with the corresponding View Controllers.Make sure you link these VCs with Storyboard IDs
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        let vcName = identities[indexPath.row]
        let viewController = storyboard?.instantiateViewControllerWithIdentifier(vcName)
        self.navigationController?.pushViewController(viewController!, animated: true)

Try these codes.Hope you guys get what i tried to explain and Enjoy programming Swift!!

Please let me know if you have any queries related to this by commenting below.



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s