1

I have such view hierarchy:

View Controller

UITableView

UITableViewCell

UITextField

I have in my custom UITableViewCell a textField and my plan is to make this textField becomeFirstResponder as soon as user tap on cell. The first problem is that textField is getting touches events and cell is not selected because of that so when user tap right onto the textField then cell is not selected and I'm not able to update selected cell UI. I though that I can make isUserInteractionEnabled=false on textField so after user taps on textField I will have proper selection behavior in my cells. So far so good. Now I want to handle this selection in setSelected method in my custom cell. The problem is that if keyboard is already on screen and I execute becomeFirstResponder() on textField then the keyboard is hiding and immediately showing again. Normally I expect that this keyboard will stay on its place without trying to hide and show again. Here is the code:

class StringFieldTableViewCell: UITableViewCell {

    @IBOutlet weak var pinLine: UIView!
    @IBOutlet weak var pinLineFocused: UIView!
    @IBOutlet weak var textField: UITextField!


    override func awakeFromNib() {
        super.awakeFromNib()
        self.selectionStyle = .none
        self.textField.isUserInteractionEnabled = false
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        self.textField.isUserInteractionEnabled = selected
        if selected {
            self.textField.becomeFirstResponder()
        }
        self.pinLine.isHidden = selected
        self.pinLineFocused.isHidden = !selected
    }
}

Does anybody knows how to fix this keyboard showing/hiding problem and why keyboard tries first to hide itself and then show in very short time?

dahiya_boy
  • 9,298
  • 1
  • 30
  • 51
Marcin Kapusta
  • 5,076
  • 3
  • 38
  • 55
  • Possible duplicate of [UITableViewCell with UITextField losing the ability to select UITableView row?](https://stackoverflow.com/questions/6579904/uitableviewcell-with-uitextfield-losing-the-ability-to-select-uitableview-row) – Rakesha Shastri Jan 03 '19 at 13:18
  • The most simple way in this case is to check textField.isFirstResponder – IBAction Jan 03 '19 at 13:21
  • No this is not the duplicate. I saw this before and it didn't help me with hiding/showing keyboard problem. @IBAction How `isFirstResponder` can help me with this Keyboard issue that I described? – Marcin Kapusta Jan 03 '19 at 13:24
  • 1
    if selected && !textField.isFirstResponder { textField.becomeFirstResponder() } – IBAction Jan 03 '19 at 13:27
  • Hmm. It does not work. The keyboard is hiding and showing when I'm selecting different cells. – Marcin Kapusta Jan 03 '19 at 13:31
  • Yes but When You have many textFields with `isUserInteractionEnabled = true` and You are tapping on them in any order the keyboard is not hiding and showing. It just stays visible all the time during switching between textFields... – Marcin Kapusta Jan 03 '19 at 13:36

1 Answers1

0

The problem in your code is that you are kind of disabling the textField interaction because you have textField in your cells and if you tap on the cell you will interact with the textField, not the cell itself.

So there is a way to select a row even if you have a textField in your cell.

First, you need to get when you tapped on that textField.

for that you can use this....

let pointInTable = textField.convert(textField.bounds.origin, to: self.tableView)

It will give you a value in a CGPoint([x, y]), for E.g [0.0, 0.0] or [0.0, 44.0]. Depending upon the height of your Cell.

Now, there is a function that converts CGPoint to IndexPath.

let textFieldIndexPath = tableView.indexPathForRow(at: pointInTable)

Now this will be our indexPath.

Now you can use cellSelected Fuction.

tableView.selectRow(at: textFieldIndexPath, animated: true, scrollPosition: .none)

The whole code will be like this:

let pointInTable = textField.convert(textField.bounds.origin, to: self.tableView)
let textFieldIndexPath = self.tableView.indexPathForRow(at: pointInTable)

tableView.selectRow(at: textFieldIndexPath, animated: true, scrollPosition: .none)

You can put this in textFieldDidBeginEditing Function.

Simran Singh
  • 445
  • 6
  • 8