ChatGPT解决这个技术问题 Extra ChatGPT

UITableView didSelectRowAtIndexPath: not being called on first tap

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?

Can you post your code for tableView:didSelectRowAtIndexPath:?
I had the same problem, check my answer: stackoverflow.com/a/44097559/1600061
Dispatch in main queue solved my problem stackoverflow.com/questions/20149167/…

O
Ole Begemann

Any chance you accidentally typed didDeselectRowAtIndexPath?


@Dschee it's not called on the first tap because you are not deselecting anything. Once you selected one row, when you try to select a second one, the first gets deselected.
That's why I LOVE SO.
I upvoted this not because I have made this mistake once. I upvoted because I have made this mistake no less than 4 times in just as many years, costing so much time.
OMG!! This might be the worst bug that I have introduced in my code.
WOW. Saved me so much time.
i
includeMe

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


Ba ha ha ha! Wow I spent 4 hours trying to figure this out. @includeMe to the rescue (salute)! Thanks!
s
sonxurxo

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:

https://i.stack.imgur.com/WX3bQ.png

Hope that helps someone


You are my hero! Any idea why this is the default? I can't think of any app having that behavior and I think this is a very weird default.
Y
YSR fan

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


I have both; Gesture recognizer and didSelectRowAtIndexPath? However, always the gesture recognizer takes preference. Unable to get to the second one. Is there a way to handle both or do I have to get rid of gesture recognizer? Thanks!
this must be at top :) it cost me 1 hour after 7 years in iOS development.
It cost me a lot more than an hour... Agreed, it should be at top! Thank you @YSR fan
C
Chase Roberts

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).


Did you find the reason why this is happening? I have exactly the same issue. When I turn off table bouncing, then the first / last row in the table does not react on the first tap. I thought it's bug but it's over one year and it's still happening.
No. I just turned on the tableview bounce and left it at that.
I'm having this issue on an iPhone 5 only (iOS 10.3) - turning ON bounce on my tableview fixed it. Very odd!
Due to a system bug/misfeature, buttons at the bottom of the screen don't fire correctly due to system GestureRecognizers that are installed over the buttons. Fix here: stackoverflow.com/questions/23046539/…
A
Anton Tropashko

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.


This worked for me but I need a workaround for hiding the selection without making its style None. I did override setSelected but while the user is holding the cell, the selection appears which is not acceptable
A
Ayoub Nouri

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.


I am using table view inside gesture. As per the animation requirements I have to do this. So I am also facing same issue. I have to tap twice to select row at very first time. It get resolved after using gesture.cancelsTouchesInView = false Thanks.
L
Lukesivi

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()


Z
ZGski

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.


D
Diogo Martins

SWIFT 3

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

tableView.delaysContentTouches = false


g
garridozh

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.


A
Aviv Mor

Swift 4.2

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

https://i.stack.imgur.com/AGnGH.png


R
Rishil Patel

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


K
Kabeer

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


s
safari yellow

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!!


A
Amir Ehsanijam

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")
    }