UITableView didSelectRowAtIndexPath: not being called on first tap

UitableviewCocoa TouchUikitDidselectrowatindexpath

Uitableview Problem Overview


I'm having an issue with UITableView's didSelectRowAtIndexPath.

My table is setup so that when I select row it initializes a new view controller and pushes it.

The first time I tap any row in the table, the method does not get called. Once I select another row, it begins to work as normal.

I have verified this by setting a breakpoint on didSelectRowAtIndexPath. When adding an NSLog to the method I see that when I select the second row that finally pushes the new view controller, I see two log statements appear in the console at the same time.

Any suggestions?

Uitableview Solutions


Solution 1 - Uitableview

Any chance you accidentally typed didDeselectRowAtIndexPath?

Solution 2 - Uitableview

Also check the selection property of your table view in xib file. Use 'Single Selection' or 'Multiple Selection' as required.

Solution 3 - Uitableview

I experienced the following issue:

  • first tap in row -> no effect, no selection, never
  • second tap and following -> correct selection behavior, always

In my case, my error was checking Show Selection on Touch in Interface Builder. You can uncheck it in IB here:

enter image description here

Hope that helps someone

Solution 4 - Uitableview

Check If you have set any Gesture recognisers in your class. Removing gesture worked for me.

Solution 5 - Uitableview

I debated even posting this answer because I think the stars kind of aligned in order for this to manifest itself.

I am having a variation of this problem and have checked the other solutions. On my table view it isn't processing the very last row of my table on the first tap. It highlights it, but didSelectRowAtIndexPath isn't being called. All the other rows work fine. But if I turn the tableview bounce on then it seems to solve the problem (but then you have to deal with a tableview bounce).

Solution 6 - Uitableview

UITableViewCellSelectionStyleNone was set for the cell displaying that problem (ios9).

I have ended up calling

[tableView deselectRowAtIndexPath:indexPath animated:NO];

first thing in

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

which is not the same, but is good enough. makes me wonder what kind of table breakage ios10 brings.

Solution 7 - Uitableview

this issue happens also when you are working with gesture recogniser within a tableView in this case you don't need to remove them, you need only to make sure that your gesture property cancelsTouchesInView = false this is a boolean value affecting whether touches are delivered to a view when a gesture is recognised.

Solution 8 - Uitableview

SWIFT 2

Make sure you have this set to true:

self.tableView.allowsSelection = true

Put this above your right after your viewDidLoad() and before the super.viewDidLoad()

Solution 9 - Uitableview

SWIFT 3

If you are working with Swift 3 in a class which isn't a UITableViewController and you are using UITableViewDelegate, then you may need to use the method title:

@objc(tableView:didSelectRowAtIndexPath:) func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){...}

This worked for me, although I have just migrated my project to Swift 3. It's fairly new so this may be fixed later.

Solution 10 - Uitableview

SWIFT 3

None of the other answers worked in my case, what fixed it for me was:

tableView.delaysContentTouches = false

Solution 11 - Uitableview

If you have set any UITapGestureRecognizer in your class, you can add this line in your didSelectRowAtIndexPath:

[tableView deselectRowAtIndexPath:indexPath animated:NO];

This worked for me.

Solution 12 - Uitableview

Swift 4.2

Disabling "Delay Touch Down" in the attributes inspector solved the issue.
After that the clicks are smooth and didSelectRowAt fires immediately.

XCode attributes inspector

Solution 13 - Uitableview

didSelectRowAt function was not called in my app in first or second tap... I was trying to solve the problem could not find any solution. But suddenly I was recognise, I was using view.animation color change... Delegate method was not called while animation persist

Solution 14 - Uitableview

In my case I had a UITableView section header with a gesture recognizer. When the header is tapped, it should insert few rows into that section and animate the insertion (like expand/collapse section). First time when expanded and tapped, the didSelectRow delegate method was not fired. For further taps and expand/collapse actions, it was working as expected.

Per answer by @Ayoub Nouri I set cancelsTouchesInView to false, and this resolved the issue. tapGestureRecognizer?.cancelsTouchesInView = false

Solution 15 - Uitableview

What worked for me was to start typing "didSelect..." and then let autocorrect fill in the rest. Apparently some detail of my syntax was wonky. As others have said, Use the IDE!!

Solution 16 - Uitableview

I had the same issue on my tableView(swift 4.2), it could be fixed with allowsMultipleSelection property on tableView.

If allowsMultipleSelection is set to true the table view selection mechanism will change in a way that by selecting each cell the tableView didSelectRowAtIndexPath: is called for the first time and by selecting the same cell for the second time the tableView didDeselectRowAtIndexPath: is called.

It means that if the number of times a cell tapped are odd (1, 3, 5, ...) then always tableView didSelectRowAtIndexPath: will be called and if the number of times a cell tapped are even (2, 4, 6, ...) then always tableView didDeselectRowAtIndexPath: will be called.

This makes tableView didSelectRowAtIndexPath: function to be called on the third selection for the same cell and so the result is double tap for calling didSelectRowAtIndexPath:!!!

If you want the tableView didSelectRowAtIndexPath: to be called on each selection for a cell then the tableView multiple selection has to be set false, tableView.allowsMultipleSelection = false.

By doing this, every time the cell is tapped tableView didSelectRowAtIndexPath: will be called on table view and by selecting another cell tableView didDeselectRowAtIndexPath: will be called for the cell was selected before and then the tableView didSelectRowAtIndexPath: will be called for the newly selected cell.

    class TableViewController: UITableViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.allowsMultipleSelection = false
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("This will be called for each cell tap")
    }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMark AdamsView Question on Stackoverflow
Solution 1 - UitableviewOle BegemannView Answer on Stackoverflow
Solution 2 - UitableviewincludeMeView Answer on Stackoverflow
Solution 3 - UitableviewsonxurxoView Answer on Stackoverflow
Solution 4 - UitableviewYSR fanView Answer on Stackoverflow
Solution 5 - UitableviewChase RobertsView Answer on Stackoverflow
Solution 6 - UitableviewAnton TropashkoView Answer on Stackoverflow
Solution 7 - UitableviewAyoub NouriView Answer on Stackoverflow
Solution 8 - UitableviewLukesiviView Answer on Stackoverflow
Solution 9 - UitableviewAdam SmithView Answer on Stackoverflow
Solution 10 - UitableviewDiogo MartinsView Answer on Stackoverflow
Solution 11 - UitableviewgarridozhView Answer on Stackoverflow
Solution 12 - UitableviewAviv MorView Answer on Stackoverflow
Solution 13 - UitableviewUcdemirView Answer on Stackoverflow
Solution 14 - UitableviewKabeerView Answer on Stackoverflow
Solution 15 - Uitableviewsafari yellowView Answer on Stackoverflow
Solution 16 - UitableviewAmir EhsanijamView Answer on Stackoverflow