Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableView didSelectRowAtIndexPath sometimes called after second tap

I'm facing a curious UITableView behaviour and i don't know where this is coming from. I'm building a very simple single view IOS8 Swift application with a first ViewController with a UITableView inside it and one custom Image cell. When i tap on a cell it Segue to my SecondViewController.

My UITableView delegate and datasource is connected to the first ViewController. Everything is working except when i tap a cell sometimes i have to tap it twice to trigger the Segue.

Here is my didSelectRowAtIndexPath function:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    println("You clicked on \(indexPath.row) row")
    self.performSegueWithIdentifier("homeToDetail", sender:self)
}

My print is always returning the correct indexPath. Is it a view hierarchy problem or something else ?

Thank you JD

like image 365
Jonnyx Delavilla Avatar asked Jan 12 '15 16:01

Jonnyx Delavilla


People also ask

What is UITableView in Swift?

A view that presents data using rows in a single column. iOS 2.0+ iPadOS 2.0+ Mac Catalyst 13.1+ tvOS 9.0+

What is IndexPath in tableView Swift?

Swift version: 5.6. Index paths describe an item's position inside a table view or collection view, storing both its section and its position inside that section.

What is Table View delegate?

func tableView(UITableView, willDisplay: UITableViewCell, forRowAt: IndexPath) Tells the delegate the table view is about to draw a cell for a particular row. func tableView(UITableView, indentationLevelForRowAt: IndexPath) -> Int. Asks the delegate to return the level of indentation for a row in a given section.


2 Answers

Try this.

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
  dispatch_async(dispatch_get_main_queue(), {
    self.performSegueWithIdentifier("homeToDetail", sender:self)
  })
}

This is a bug in iOS 8, I think. UITableViewCell selection Storyboard segue is slow - double tapping works though

like image 86
rakeshbs Avatar answered Nov 15 '22 12:11

rakeshbs


You have to call [tableView deselectRowAtIndexPath:indexPath animated:YES]; to fix this

like image 28
daleijn Avatar answered Nov 15 '22 14:11

daleijn