1

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 Answers1

1

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

  • Thank you for your answer. I understand the concept and tried to call that method every time I reload the tableview and colletionview cells. However, the keyboard resigns first and then shows back up. I hope the keyboard just stays without resigning when I tap the tableview cells or collectionview cells. I hope the keyboard only resigns when I start dragging the cells... Any further thoughts would be greatly appreciated. – Gilbert Tyler Jun 19 '20 at 18:27
  • Thank you very much. I thought that was a great idea. I implemented it immediately and found that it did work in the sense that the keyboard does not resign any more when I tap the cells. However, another issue surfaced. Now even after I drag the keyboard to resign, the cursor in the textfield does not disappear... I would appreciate your thoughts. – Gilbert Tyler Jun 20 '20 at 05:01
  • Hello! Can you try calling "textField.resignFirstResponder()" in the "else" statement in step 6 and let me know if it works? I honestly I am not sure why this is happening, but maybe this will work. + if you have a GitHub page, I could take a look and see if I can suggest further solutions. – Abdulelah Hajjar Jun 20 '20 at 14:18
  • Thanks a lot for your help. I tried that, but it does not make any difference. I figured what the problem was at least... – Gilbert Tyler Jun 20 '20 at 18:41
  • The thing is once isCellBeingClicked variable switches to "true", there is no way of switching it back to "false" status. If I add the code to make the isCellBeingClicked variable "false" right after all the table view cells are reloaded, it seems that the true-to-false switch gets implemented before textFieldShouldEndEditing method is called. – Gilbert Tyler Jun 20 '20 at 18:41
  • Perhaps if there is a way to keep the isCellBeingClicked variable as false only until textFieldShouldEndEditing is called and then if I can change the variable to "true" right after textFieldShouldEndEditing is called, that would solve the problem... I am wondering if you have any final thought. Thanks again for your help. Very much appreciated. – Gilbert Tyler Jun 20 '20 at 18:42
  • I was having same same problem when i disable user interaction. so this approach helped me to solve. Thank you. – Yılmaz edis Aug 31 '23 at 08:55