UITableView didSelectRowAtIndexPath: not being called on first tap

巧了我就是萌 提交于 2019-11-26 14:53:17

Any chance you accidentally typed didDeselectRowAtIndexPath?

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

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:

Hope that helps someone

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

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

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.

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

Adam Smith

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.

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.

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.

SWIFT 3

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

tableView.delaysContentTouches = false

Beyaz

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

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

Swift 4.2

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!