Aniket Patil Aniket Patil - 2 months ago 27
Swift Question

UITapGestureRecognizer breaks UITableView didSelectRowAtIndexPath in SearchField Class in Swift?

I am using this library for SearchText Field

https://github.com/apasccon/SearchTextField

Here is my Code :

override func viewDidLoad() {
super.viewDidLoad()


self.view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: Selector("dismissKeyboard")))


}

func dismissKeyboard()
{
ClientList.resignFirstResponder()
self.view.endEditing(true)

}


Dismiss of keyboard is working but the didSelectRowAtIndexPath is not getting called so i am not able to select anything

Image for Reference : enter image description here

Partial Solution I Done:

func hideKeyboard(gestureRecognizer: UIGestureRecognizer) {
let point = gestureRecognizer.locationInView(ClientList.tableView)
let indexPath = ClientList.tableView!.indexPathForRowAtPoint(point)

if indexPath != nil {
return
}
if ClientList.becomeFirstResponder() {
ClientList.resignFirstResponder()
}

}


But now issue is i have other list too , how can i implement for other list ?

Answer

You can solve this issue like this

ViewDidLoad

self.tap = UITapGestureRecognizer(target: self, action: "viewTapped:")
self.tap.delegate = self
self.view.addGestureRecognizer(self.tap)

Function

func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldReceiveTouch touch: UITouch) -> Bool {
    if touch.view != nil && touch.view!.isDescendantOfView(self.tableView) {
        return false
    }
    return true
}
Comments