Simplified iOS

  • Home
  • About
  • Contact
  • Advertise
  • Write for Us

iOS Table View Tutorial – Creating TableView in Xcode 7 using Swift

July 30, 2016 by Belal Khan Leave a Comment

Hello friends welcome to iOS Table View Tutorial. In the last tutorial we learnt about taking user input using Text Field. Today we will learn creating a simple Table View in iOS using Swift in Xcode 7. So lets begin this iOS Table View Tutorial.

What is Table View?

The name is little ambiguous for me 😛 As the same thing in Android is known as a ListView.  So a Table View is one of the most common element in iOS apps and it is used to display list of data.

In the previous tutorials we did some simple things but today we will learn about Table View which is a bit more complex. Below you can see a simple Table View in iOS.

ios tableview tutorial

We will be creating the same in this tutorial. So lets start our iOS Table View Tutorial.

iOS Table View Tutorial

So lets start our iOS Table View Tutorial.  We will start with creating a new Xcode project. Start following these steps.

Creating a new Xcode Project

  • Launch Xcode and create a new project.

ios table view tutorial

  • Now you need to select  Single View App and click on next.

singleview app

  • Now give your app a name, I have given TableViewExample. You can put your Organization Name and Identifier or you can also leave the defaults value. After filling the values click on Next again.

ios table view tutorial

  • Now you will be asked to select a location to store your project. You can choose any location and then click on create.

create xcode project

  • Now your project will be loaded.

ios table view tutorial

  • Now click on Main.storyboard and unmark Use Size Classes.

Adding Table View Controller to Storyboard

  • Click on Main.storyboard.

ios simple tableview

  • Here you can see your current View Controller. Just click on it and Press Delete to delete this View Controller.
  • Now from the left find Table View Controller.

ios tableview tutorial

  • After dragging the Table View Controller to story board you will see your Table View Controller.

simple ios table view example

  • Here you need to mark your Table View Controller as Initial View Controller. (See the above image for explanation).
  • Now click on ViewController.swift and modify the code as follow.
//
//  ViewController.swift
//  TableViewExample
//
//  Created by Belal Khan on 30/07/16.
//  Copyright © 2016 Belal Khan. All rights reserved.
//

import UIKit

//Changed the class name to TableViewController
//and it is inheriting the class UITableViewController
class TableViewController: UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

  • We haven’t done much we just changed the class name to TableViewController and it is inheriting the class UITableViewController.
  • Now in identity inspector we will add our class.

ios table view tutorial

  • The above image explaining what you need to do. Basically we are adding our class to our Table View Controller.
  • Now we need to assign the cell an identifier.

ios table view tutorial

  • You can put any name in identifier, just remember it is case sensitive.
  • Now we will add some data to our Table View.

Adding Data to Table View

  • Come inside ViewController.swift  and modify the code as follow.
//
//  ViewController.swift
//  TableViewExample
//
//  Created by Belal Khan on 30/07/16.
//  Copyright © 2016 Belal Khan. All rights reserved.
//

import UIKit

//Changed the class name to TableViewController
//and it is inheriting the class UITableViewController
class TableViewController: UITableViewController {

    //created a string array to display on table view
    var tableItems = ["Swift","Python","PHP","Java","JavaScript","C#"]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    //this method will populate the table view
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let tableRow = tableView.dequeueReusableCellWithIdentifier("TableRow") as UITableViewCell!
        
        //adding the item to table row
        tableRow.textLabel?.text = tableItems[indexPath.row]
        
        return tableRow
    }
    
    
    //this method will return the total rows count in the table view
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return tableItems.count
    }


}

  • Now try executing your application.

ios tableview tutorial

  • Bingo! its working absolutely fine.
  • If you are having confusions, you can download the source code from the below link.

[sociallocker] iOS Table View Example (1190 downloads)  [/sociallocker]

So thats all for this iOS Table View Tutorial friends. Feel free to ask your queries by leaving your comments. Thank You 🙂

Share this:

  • Tweet
  • Share on Tumblr
  • WhatsApp

Related

Filed Under: iOS Development Tutorial Tagged With: ios table view tutorial

About Belal Khan

I am Belal Khan, I am currently pursuing my MCA. In this blog I write tutorials and articles related to coding, app development, iphone etc.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *




About Me

belal khan simplified ios

Hello I am Belal Khan, founder and owner of Simplified iOS. I am currently pursuing MCA from St. Xavier's College, Ranchi. Apart from my academic I am a blogger, I run various websites and majority of them are about coding and development.

Connect with Me

Follow Simplified iOS

Simplified iOS

Popular Posts

  • Swift SQLite Tutorial for Beginners – Using… (99,458)
  • Swift PHP MySQL Tutorial – Connecting iOS App… (98,347)
  • UIWebView Example to Load URL in iOS using Swift in Xcode (80,016)
  • Download Full High Sierra Installer to Create Bootable USB (70,345)
  • Xcode Login Screen Example using Swift 3, PHP and MySQL (67,312)
  • How to Format USB on Mac? Formatting External Hard… (61,391)
  • Swift JSON Tutorial – Fetching and Parsing… (59,413)
  • Firebase Realtime Database Tutorial for Swift using Xcode (53,721)
  • iOS Registration Form Example using PHP and MySQL (48,988)
  • Xcode Text Field Tutorial for iOS Application using Swift (41,226)




About

Simplified iOS is a blog where you can find latest tutorials related to coding and app development for iphone and MAC OS. Here you can get Simplified iPhone, iPad and Mac OS development tutorial to know the things about Apple Development from basics to advanced level.

Quick Links

  • Advertise
  • Contact
  • Disclaimer
  • Privacy Policy
  • Write for Us

Copyright © 2017 · Simplified iOS· All rights Reserved. And Our Sitemap.All Logos & Trademark Belongs To Their Respective Owners·