2

I have TableViewController with UITextFields as cells and cells without TextField.
I would like to hide the keyboard when I tapped on another cell without TextField

PrakashG
  • 1,642
  • 5
  • 20
  • 30
AlexLeo
  • 89
  • 1
  • 1
  • 9
  • 2
    Possible duplicate of [Close iOS Keyboard by touching anywhere using Swift](https://stackoverflow.com/questions/24126678/close-ios-keyboard-by-touching-anywhere-using-swift) – Cedan Misquith Jun 20 '19 at 07:37
  • I would suggest you use https://github.com/xmartlabs/Eureka / XLForm based on your need. This automatically handles all the needs of Keyboard management and makes life easier. – Pavan Kotesh Jun 20 '19 at 07:37

4 Answers4

2

In your UITableViewDelegate implementation:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    view.endEditing(true)
}
vdmzz
  • 261
  • 1
  • 4
2

try using "IQKeyboardManagerSwift", it has all the keyboard controls that you will need in the future.

1

This could be helpful-

extension YourViewController: UITableViewDelegate {

        func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            self.view.endEditing(true)
        }
    }
Anupam Mishra
  • 3,408
  • 5
  • 35
  • 63
1

You have two ways to handle it, first you can hide keyboard when the user tapped on cells without TextField, by table view delegate, but you should ignore the rows that had textfiled

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    view.endEditing(true)
}

Or you can add UITapGestureRecognizer to the cells that dose not had text field

func hideKeyboardWhenTappedAround() {
    let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UITableViewCell.dismissKeyboard))

    view.addGestureRecognizer(tap)
}

@objc func dismissKeyboard() {
    view.endEditing(true)
}
A.Munzer
  • 1,890
  • 1
  • 16
  • 27