Skip to content

Instantly share code, notes, and snippets.

@ordovician
Last active October 3, 2018 23:19
Show Gist options
  • Save ordovician/92dd8941d41d9c350c7c to your computer and use it in GitHub Desktop.
Save ordovician/92dd8941d41d9c350c7c to your computer and use it in GitHub Desktop.
[Tap anywhere in text cell] How to make UITextField first responder when tapping anywhere inside a table view cell. Just like Apple's "Settings" app. #firstresponder #viewcell
class Settings : UITableViewController {
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let cell = tableView.cellForRowAtIndexPath(indexPath)
// The cells contentview is the one containing custom added components.
if let subviews = cell?.contentView.subviews {
// Find a subview which is a UITextField. Place cursor in this textfield.
for view in subviews {
if let textfield = view as? UITextField {
textfield.becomeFirstResponder()
break
}
}
}
}
}
@galahador
Copy link

let Yourcell = tableView.cellForRow(at: indexPath)
    if let cell = cell as? CustomTableViewCell{
        DispatchQueue.global(qos: .userInitiated).async {
            DispatchQueue.main.async {
                cell.cellText.becomeFirstResponder()
            }
        }
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment