UITapGestureRecognizer在Swift中的SearchField类中打破UITableView didSelectRowAtIndexPath?

时间:2016-08-15 11:43:20

标签: ios swift uitableview uisearchbar uitapgesturerecognizer

我正在将此用于SearchText字段

https://github.com/apasccon/SearchTextField

这是我的代码:

  override func viewDidLoad() {
    super.viewDidLoad()


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


}

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

}

关闭键盘工作但是没有调用didSelectRowAtIndexPath所以我无法选择任何东西

参考图片:enter image description here

我做的部分解决方案:

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

    }

但现在问题是我还有其他列表,我该如何实现其他列表?

3 个答案:

答案 0 :(得分:2)

你可以像这样解决这个问题

<强> viewDidLoad中

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

<强>功能

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

答案 1 :(得分:0)

谁是这个姿势的目标?我认为它应该是列表,因为它是你点击的,它在视图上方

答案 2 :(得分:0)

        let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "dismissKeyboard")
    view.addGestureRecognizer(tap)

将此添加到viewDidLoad

并添加此功能

func dismissKeyboard() {
    //Causes the view (or one of its embedded text fields) to resign the first responder status.
    view.endEditing(true)
}