Simplified iOS

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

iOS TableView Swift Tutorial – Handling Clicks in TableView Cell

August 7, 2016 by Belal Khan 2 Comments

Hello friends in this post we will again learn something about iOS TableView. We have already seen how to create an iOS TableView in one of our previous post.

Today in this iOS TableView Tutorial we will learn how we can handle the clicks in TableView Cells. Before moving on this tutorial I will recommend you to go through the previous UITableViewController Tutorial Swift first. Because I will be working on the same project we created on the last tutorial.

Creating a Simple iOS TableView

  • We already have the Table View ready. If you don’t created the Table View yet, then go to the last iOS Table View Tutorial and create a Table View.
  • Now we can handle the clicks on our iOS TableView Cells.
  • Right now we have the following Table View in your App.

ios tableview tutorial

  • Now we want that whenever we select any item from the Table View an alert dialog should open displaying the value of selected cell.
  • I have already posted about how to create an alert dialog in iOS. You should also check this tutorial before moving ahead.
  • So the current code we have in our ViewController.swift is.
//
//  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 lets move ahead on handling clicks.

Handling Clicks of iOS TableView Cell

  • Handling clicks are very easy we just need to override the following method in our ViewController.swift file.
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
       //code to execute on click
    }
  • The above method will execute on tapping a table cell of our iOS TableView.
  • Inside the method we can create our Alert Dialog. And to show the text of the selected row we can use the following code inside the same method.
        //getting the index path of selected row
        let indexPath = tableView.indexPathForSelectedRow
        
        //getting the current cell from the index path
        let currentCell = tableView.cellForRowAtIndexPath(indexPath!)! as UITableViewCell
        
        //getting the text of that cell
  • Now to show alert dialog we can use the code of the last tutorial about alert dialog in iOS.
  • So the final code of our ViewController.swift file would be.
//
//  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
    }

    
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        //getting the index path of selected row
        let indexPath = tableView.indexPathForSelectedRow
        
        //getting the current cell from the index path
        let currentCell = tableView.cellForRowAtIndexPath(indexPath!)! as UITableViewCell
        
        //getting the text of that cell
        let currentItem = currentCell.textLabel!.text
        
        let alertController = UIAlertController(title: "Simplified iOS", message: "You Selected " + currentItem! , preferredStyle: .Alert)
        let defaultAction = UIAlertAction(title: "Close Alert", style: .Default, handler: nil)
        alertController.addAction(defaultAction)
        
        presentViewController(alertController, animated: true, completion: nil)
    }

}

  • Now run your app in Simulator.
ios tableview

iOS TableView

  • Bingo! its working absolutely fine.

So thats all for this iOS TableView Tutorial friends. Please share this post if you found it useful. And don’t hesitate to ask with your comments if having any confusions related to this tutorial.

Stay tuned for more iPhone App Development Tutorials. Thank You 🙂

Share this:

  • Tweet
  • Share on Tumblr
  • WhatsApp

Related

Filed Under: iOS Development Tutorial Tagged With: ios table view swift, ios tableview cell clicks, ios tableview 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.

Comments

  1. Justin says

    January 20, 2017 at 12:52 am

    You don’t need this line:

    let indexPath = tableView.indexPathForSelectedRow

    It is already passed in via the event as the parameter indexPath

    Reply
  2. SwiftNovice says

    March 30, 2017 at 7:06 pm

    Hi Belal,

    This works pretty well, thanks!

    In my tableView I also have a search bar and it searches for items and filters.
    Would you be able to advise how I can do it so that when items are filtered, they still allow popup to come up?

    SO for example, I had 10 rows, and with search filter, I now have 6 – how would I make sure that the popup still appears for those items ?

    Thanks

    Reply

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,463)
  • Swift PHP MySQL Tutorial – Connecting iOS App… (98,351)
  • UIWebView Example to Load URL in iOS using Swift in Xcode (80,016)
  • Download Full High Sierra Installer to Create Bootable USB (70,346)
  • Xcode Login Screen Example using Swift 3, PHP and MySQL (67,312)
  • How to Format USB on Mac? Formatting External Hard… (61,392)
  • 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,989)
  • Xcode Text Field Tutorial for iOS Application using Swift (41,228)




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·