1

I am new to Swift and have been struggling with hiding the keyboard when the textField is on a custom tableview cell. I think the problem stems from the textField Reference being in the TableViewCell class, but i can't be sure. I have tried everything and am a little lost.

My code consists of:

TableViewCell:

import UIKit

class TableViewCell: UITableViewCell, UITextFieldDelegate
{
    @IBOutlet weak var userText: UITextField!
    @IBAction func answers(_ sender: UITextField)
    {
    }

    override func setSelected(_ selected: Bool, animated: Bool)
    {
        super.setSelected(selected, animated: animated)
    }

    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.delegate = self
        textField.resignFirstResponder()
        return true
    }
}

and TableViewController:

import UIKit

class TableViewController: UITableViewController, UITextFieldDelegate
{
    var textfield = TableViewCell()

    override func viewDidLoad()
    {
        super.viewDidLoad()
        let myText = textfield.userText
        textField.resignFirstResponder()
        myText?.delegate = self
    }

    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        self.view.endEditing(true)
        return false
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        return 3
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)

        return cell
    }
}

I tried running the textFieldShouldReturn function from both classes but I can't get it to work.

lisen kaci
  • 151
  • 1
  • 12
  • Possible duplicate of [Close iOS Keyboard by touching anywhere using Swift](https://stackoverflow.com/questions/24126678/close-ios-keyboard-by-touching-anywhere-using-swift) – Adrian May 30 '17 at 00:20
  • Have you tried the textField.resignFirstResponder? – Ivan Cantarino May 30 '17 at 00:33

3 Answers3

0

Inside your textFieldShouldReturn function insert textField.resignFirstResponder

Or in your viewDidLoad insert tableView.keyboardDismissMode = .onDrag or .interactive so it dismisses when you scroll if you want

Ivan Cantarino
  • 3,058
  • 4
  • 34
  • 73
  • func textFieldShouldReturn(_ textField: UITextField) -> Bool { textField.resignFirstResponder() self.view.endEditing(true) return true } still did not fix it for me – lisen kaci May 30 '17 at 00:42
  • I think it has something to do with the reference for for textField being in the separate TableViewCell class but i cant be sure or how to fix it. – lisen kaci May 30 '17 at 00:45
  • Have you tried the dismiss on drag option? That's the most commonly used nowadays just swipe down and it closes the keyboard – Ivan Cantarino May 30 '17 at 00:45
  • I thought the dismiss on drag was for scrollView I'm not sure how to implement it for a tableView I would still need the keyboard to dismiss on return though. – lisen kaci May 30 '17 at 00:55
0

Add this function in your tableView cell class because you have declared delegate in tableviewCell's class.

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
      textField. resignFirstResponder() 
        return false
    }
KKRocks
  • 8,222
  • 1
  • 18
  • 84
0

remove UITextFieldDelegate and delegate func from UITableViewCell then set delegate in UITableViewController->inside

`override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)as! TableViewCell

    cell.userText.delegate = self
    return cell
}

then you want to add textFieldShouldReturn in UITableViewController and return true try this