QuantumHoneybees QuantumHoneybees - 1 year ago 221
Swift Question

Dismiss keyboard when uitableview cell is selected and using UISearchController

So I have an app that is basically a movie search app displayed using a UITableViewController and a UISearchController.

I have a cell that when tapped, loads more results. But when I tap it, I also want the keyboard to go away. So in the

tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
method, I assume I have to call
. The problem is that it doesn't work. I've tried everything but nothing makes the keyboard disappear. I think it's because the keyboard is part of the UISearchBar, and I should call
. The problem is that I can't access the searchBar from my

Here is what I have, none of this works:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
//Remove Keyboard

//Do a bunch of other stuff..

Answer Source

If you know that the searchBar is the first responder then either:

  1. Pass a reference to the searchBar to the UITableViewController
  2. Use KVC: Post a notification that you're loading more results and keyboard should be hidden, and in your UISeachController listen for that notification.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download