How could I keep keyboard up when tableview cells are tapped?

1.2k Views Asked by At

I am a beginner learning Swift and trying to build a search page with Swift. In my search page of the app, I have added two Views in my storyboard with one View above the other.

  • The upper View contains a Collection View where I have two prototypes of collection view cells. The first type of the cells has Label. The second type of the cells has TextField.
  • The other View on the bottom half of the screen contains a dynamic Table View where I have a list of items that can be selected. Each row of the table view cells has a selection item.

So, when I tap on a table view cell, the selection item will appear in the collection view. If I type a keyword in the TextField in the collection view, table view reloads and shows all the selection items that has the keyword, so I can tap and add an item to the collection view.

I would like to keep adding by typing a keyword after I tap on a searched item in the table view. So, I made the first cell showing selected items with labels and the second cell that has the TextField separated into two sections of the collection view. So, I only reload the first section (without TextField) for each selection. But somehow the keyboard automatically resign whenever I tap on the table view cell to add an item to the collection view.

Is there any way I can keep the keyboard up even when I tap on the tableview cells?

The keyboard also resigns when I tap the collection view cells.

I would appreciate your advice. Thanks.

1

There are 1 best solutions below

7
Abdulelah Hajjar On

I hope you are having a good day.

You can try calling this method on the UITextField you would like to show the keyboard for (maybe call it after the user taps on the UITableViewCell):

textField.becomeFirstResponder()

where "textField" is the variable name of your UITextField.

Please let me know if this fixed your issue.


Edit #1

Hello! Since my previous solution did not achieve your intended behavior. There is another solution in my mind, however I have not tried it before.

As an introduction to the concept of delegation, there is a method created by Apple called "textFieldShouldEndEditing" which is called by Apple whenever any keyboard will disappear on any text field.

This method is created by Apple, but you can override it (i.e. customize it) to suit your needs and tailor its behavior.

  1. To override this method you have to assign your class as the delegate of UITextField by adding UITextFieldDelegate to your class definition as follows:
class YourClassName: UIViewController, UITextFieldDelegate { }
  1. Now you have to set your class as the delegate by saying textField.delegate = self For every UITextField you create in your collection views

  2. You then can re-create the method we discussed earlier in your class:

func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
    //let's implement it the next steps, but for now, let's return true.
    return true
}

Now instead of Apple calling their version of the method, they will call yours.

  1. You then can create a variable in the top level of your class (I will let you know where this will be helpful later), and "maybe" name it as:
var isCellBeingClicked = false
  1. Now upon clicking on a cell, make this variable true, I believe you are using the method didSelectRowAt (but you could be using any other method which is fine):
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    [...]
    isCellBeingClicked = true
    [...]
}
  1. Now back to our customized method textFieldShouldEndEditing mentioned in step 3. You can add this implementation:
func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
    //If a cell is being clicked right now, please do not dismiss the keyboard.
    if isCellBeingClicked {
        isCellBeingClicked = false //reset the value otherwise the keyboard will always be there
        return false
    }
    else { return true }
}

Please let me know if this fixes your issue.

Best regards