2016-12-22 33 views
1

如我們所知,我們使用下面的代碼可以得到endEditing這個searchBar的firstResponder,但是如果有scrollView或tableView的話,效果是不同的。添加tapGesture到tableView然後不能執行tableView(_tableView:UITableView,didSelectRowAt indexPath:IndexPath)方法

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 
    super.touchesBegan(touches, with: event) 

    self.view.endEditing(true) 
} 

我添加tapGesture到的tableView,這樣我就可以endEditing搜索欄的firstResponder。

但在將tapGesture添加到我的tableView之後,tableView的tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)函數將不再起作用。

我該如何解決這個問題?


加成

我有用的代碼如下:

let tap:UITapGestureRecognizer = UITapGestureRecognizer.init(target: self, action: #selector(tapTableView)) 
self.tableView.addGestureRecognizer(tap) 


func tapTableView() { 

    self.searchBar.endEditing(true) 
} 
+0

除非你檢查它觸碰它會覆蓋全觸控的觀點,而不是你可以考慮使用內置在諸如薛志東隋衛平李利軍 – SeanLintern88

+0

@ SeanLintern88如何在tableview中的keyboardDismiss你的意思是在tableView上使用kwyboardDismiss? – aircraft

回答

0

添加tapGesture的觀點不是的tableView

let tap:UITapGestureRecognizer = UITapGestureRecognizer.init(target: self, action: #selector(tapTableView)) 
self.view.addGestureRecognizer(tap) 
+0

不,這不起作用。 – aircraft

+0

,你改變'self.searchBar.endEditing(true)'這個'self.view.endEditing(true)' –

+0

它是同一個兄弟,這個評論是無用的。 – aircraft

0

而不是在你的表視圖中添加

tableView.keyboardDismissMode = .onDrag 

您也可以將.onDrag更改爲.interactive。開始刪除觸摸。

編輯:從蘋果文檔

case none 
    The keyboard does not get dismissed with a drag. 
case onDrag 
    The keyboard is dismissed when a drag begins. 
case interactive 
    The keyboard follows the dragging touch offscreen, and can be pulled upward again to cancel the dismiss. 
+0

您能否提供更多詳細信息?我設置了'tableView.keyboardDismissMode = .onDrag'或'。互動',沒有效果,有什麼地方出錯? – aircraft

相關問題