2015-08-31 15 views
2

鍵入我的應用程序後關閉鍵盤中的tableView我有一個UITableView,其中包括在其第一小區UITextView,並在其他細胞只是UILabel秒。如何在TextView的

如何在輸入UITableView中的內容後解除鍵盤鎖定?我想在任何時候點擊其他單元格或滾動tableview來解除它。

+0

可能重複[如何解僱鍵盤的UITextView與返回鍵?](http://stackoverflow.com/questions/703754/how-to- dismiss-keyboard-for-uitextview-with-return-key) – Adrian

+0

不,這不是同一個問題。 UITextView中的返回鍵用於新行,我希望這樣。當我開始滾動UITableView或在UITextView之外的任何地方點擊時,我需要關閉鍵盤。 –

+0

http://stackoverflow.com/a/4132774/4475605 – Adrian

回答

3

您可使用符合UIScrollViewDelegateUITableViewDelegate來實現:

func scrollViewWillBeginDragging(scrollView: UIScrollView) { 
    dismissKeyboard() 
} 

func dismissKeyboard(){ 
    self.view.endEditing(true) 
} 

//Add to viewDidLoad: 
var tapGesture = UITapGestureRecognizer(target: self, action: "dismissKeyboard") 
tableView.addGestureRecognizer(tapGesture) 

//Or since you wanted to dismiss when another cell is selected use: 
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){ 
    dismissKeyboard() 
} 
+0

工作。謝謝! –

3

這裏是我如何做這個最簡單,最簡單的方法。

Dismiss keyboard property
在這裏,您可以設置屬性,只要你想的