Configuring UITableView cell text-field keyboard type

580 Views Asked by At

I'm trying to configure the cells of my table view to have different different keyboard configurations based upon what type of cell is being displayed.

In my func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell method, I have the following code to check for the value of the UILabel.text on the keyboard appropriately:

    if cell.entityLabel.text == "Home" || cell.entityLabel.text == "Mobile" {
        cell.entityTextField.keyboardType = UIKeyboardType.namePhonePad
    }

However, these cells still display the same "default" keyboard.

Any idea what I may be missing?

EDIT: I had forgotten something important! These UITableView cells are actually being created in a separate class, EditTableViewCell.swift. So, I modified the awakeFromNib() method as shown below (but still getting the same results):

override func awakeFromNib() { super.awakeFromNib() // Initialization code

if entityLabel.text == "Home" || entityLabel.text == "Mobile" {
    entityTextField.keyboardType = UIKeyboardType.namePhonePad
    entityTextField.reloadInputViews()
}



else {
    entityTextField.keyboardType = UIKeyboardType.default
    entityTextField.reloadInputViews()
}

}

1

There are 1 best solutions below

5
Artem Novichkov On

Add else statement to your expression for correct reuse logic, for example:

if cell.entityLabel.text == "Home" || cell.entityLabel.text == "Mobile" {
    cell.entityTextField.keyboardType = UIKeyboardType.namePhonePad
} else {
    cell.entityTextField.keyboardType = UIKeyboardType.emailAddress
}