Mango Ryan Mango Ryan - 1 year ago 121
iOS Question

UITextView inside a UITableViewCell, how to resign textview's firstResponder when tableView scrolls?

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"){
return false
return true

And I have no idea how to implement this method.

func scrollViewWillBeginDragging(scrollView: UIScrollView) {


My first question on stackoverflow~

Answer Source

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

tableView.keyboardDismissMode = .Interactive or .OnDrag

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download