Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift UITableView didSelectRowAtIndexPath not getting called

Tags:

ios

swift

New to IOS development and am having trouble with handling cell selection on a table. Whenever I select, the method is not getting called below - any idea why?

My project structure is: View Controller -> View -> Table View

The below code demonstrates the method calls. The others get called no problem! I know touch is working as pull down successfully refreshes and on clicking a cell it does become highlighted.

import UIKit  class ViewController: UIViewController, UITableViewDelegate {     let blah = ["blah1"]     //How many sections are in the table?    func numberOfSectionsInTableView(tableView: UITableView) -> Int {       return 1    }     //How many rows? (returns and int)    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {       return blah.count    }    //table contents for each cell?   //Each time this is called it'll return the next row and thus build a table...   func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {       print("Populating each cell of table view!\n")       tableView.rowHeight = 80.0       var cell = UITableViewCell()        var(a) = blah[indexPath.row]       var image : UIImage = UIImage(named: a)!       cell.imageView.image = image        return cell   }      //Code Cell Selected   func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){       println("You selected cell #\(indexPath.row)!")    }     func tableView(tableView: UITableViewDelegate, didDeselectRowAtIndexPath indexPath: NSIndexPath) {      print("wananananaanan" )      println("You deselected cell #\(indexPath.row)!")    }       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.   } } 
like image 705
Mark Avatar asked Mar 15 '15 19:03

Mark


People also ask

How do I populate Uitableview?

There are two main base ways to populate a tableview. The more popular is through Interface Building, using a prototype cell UI object. The other is strictly through code when you don't need a prototype cell from Interface Builder.

What is Uitableview in Swift?

A view that presents data using rows in a single column.

What is didSelectRowAt in Swift?

tableView(_:didSelectRowAt:)Tells the delegate a row is selected.

What is tableView delegate?

Methods for managing selections, configuring section headers and footers, deleting and reordering cells, and performing other actions in a table view.


2 Answers

Everybody is mentioning to set dataSource and delegate of the tableView. But after setting also not working fine then sometimes it may happen because of none or disable selection of table view.

To enable it Go to storyboard -> Select tableView -> click on the attribute inspector ->go to selector -> Select selection as single selection (or multiple selection according to the requirements.)Please find attachment

Please find attached screenshot for your suitability.

like image 179
Himani Sharma Avatar answered Sep 20 '22 14:09

Himani Sharma


You have to set an @IBOutlet to the tableView in you ViewController and set as it's delegate and dataSource to you can see the data an respond to changes in the tableView.

Something like this :

override func viewDidLoad() {     super.viewDidLoad()      self.tableView.delegate = self     self.tableView.dataSource = self } 

And implements the UITableViewDataSource protocol too.

Or you can too in the Interface Builder set the ViewController as it's delegate and dataSource (more easy to do I think) and avoid to set manually in code like above. Is up to you.

I hope this help you.

like image 33
Victor Sigler Avatar answered Sep 16 '22 14:09

Victor Sigler