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.
- 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.
- 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 🙂
Justin says
You don’t need this line:
let indexPath = tableView.indexPathForSelectedRow
It is already passed in via the event as the parameter indexPath
SwiftNovice says
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