7

I created a custom UITableViewCell, and a UITextView was put inside this prototype cell. When I click on this textView, keyboard can bounce automatically. However, if I want to hide the keyboard, I will have to click Return key. It isn't quite convenient.

Here's the question. How can I resign the textView's firstResponder for hiding the keyboard when I scroll the tableview.

cellForRowAtIndex method is below.

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell = UITableViewCell()
    if(indexPath.section == 0){
        cell = self.tableView.dequeueReusableCellWithIdentifier("InputCell")!
        let textView = cell.viewWithTag(102) as! UITextView
        textView.editable = true
        textView.keyboardType = UIKeyboardType.Default
        textView.delegate = self
        textView.targetForAction(#selector(FeedBackViewController.getTextViewInsideCell(_:)), withSender: textView)
    }
    if(indexPath.section == 1){
        cell = self.tableView.dequeueReusableCellWithIdentifier("ConfirmButtonCell")!
        let label = cell.textLabel
        label?.text = "send"
        label?.textAlignment = NSTextAlignment.Center
        label?.textColor = UIColor.redColor()
    }
    return cell
}

Here are some methods I have implemented.

func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool{
    if(text == "\n"){
        textView.resignFirstResponder()
        return false
    }
    return true
}

And I have no idea how to implement this method.

func scrollViewWillBeginDragging(scrollView: UIScrollView) {

}
Bryan
  • 14,756
  • 10
  • 70
  • 125
Mango Ryan
  • 85
  • 5

3 Answers3

7

You can try implement this on viewDidLoad, it will auto dismiss keyboard when tableview start dragging:

tableView.keyboardDismissMode = .Interactive or .OnDrag

Tj3n
  • 9,837
  • 2
  • 24
  • 35
1

UITableView inherited from UIScrollView that conforms to UIScrollViewDelegateProtocol. Try to override scrollViewDidScroll function from it and resign first responder from textview.

Be sure to assign tableView's delegate to your view controller, and then add the following code

func scrollViewDidScroll(scrollView: UIScrollView) {
    yourTextView.resignFirstResponder()
}  

As per docs scrollViewDidScroll

Tells the delegate when the user scrolls the content view within the receiver.

Update

Another approach to resign first responder is to set endEditing to true for your view:

Causes the view (or one of its embedded text fields) to resign the first responder status.

func scrollViewDidScroll(scrollView: UIScrollView) {
    self.view.endEditing(true)
} 
Evgeny Karkan
  • 8,782
  • 2
  • 32
  • 38
  • How would he access the instance of textView which is inside a tableViewCell? – NSNoob Aug 22 '16 at 12:05
  • Thanks! I have tried it too, but in this way, I can't get textView `yourTextView`inside the cell and resign it. – Mango Ryan Aug 22 '16 at 12:06
  • @EvgenyKarkan I hope you aren't going to show a method which goes around getting the instance of cell from IndexPath and then access its subviews? A simpler solution would be to call `self.view.endEditing(true)` in the scroll method. – NSNoob Aug 22 '16 at 12:08
  • By the way, first answer has resolved my problem. Thanks for your advice too~ – Mango Ryan Aug 22 '16 at 12:08
  • @NSNoob exactly - that was my second hint with end editing :) – Evgeny Karkan Aug 22 '16 at 12:11
  • @MangoRyan - awesome, now you know at least 4 ways of how to resign first responder! Thats great! – Evgeny Karkan Aug 22 '16 at 12:17
1

it is quite easy You can do same with storyboard too

open storyboard

select tableview or collection view

and select on Drag enter image description here

Prashant Tukadiya
  • 15,838
  • 4
  • 62
  • 98