1

I'm fairly new in Swift, i'm working on a dynamic table view and use reusable cell to create the view like on the screenshots my code works just fine to remove the border line of the text field, but it doesn't work when I want it to make as a first responder as soon when it comes to this page. I want to make the first row of the second section as the first responder. How do I do this?

Here's my code :

func styling() {
    for rowIndex in 0...tableView.numberOfRows(inSection: 1) {
        let indexPath = IndexPath(row: rowIndex, section: 1)
        if let editPriceCell = tableView.cellForRow(at: indexPath) as? priceListCell {
            editPriceCell.txtFieldEditPrice.becomeFirstResponder() // does not work
            editPriceCell.txtFieldEditPrice.borderStyle = .none
        }
    }
}

Here's what I want : when I move into this page, I want the first row of Change Price to becomeFirstResponder and shows up the keyboard. but it doesn't work. I have to click on the text field and then will show up the keyboard. Any solution?

Thanks for your time.

enter image description here

I have to tap the text field in order to show up the keyboard

Aldo Sugiarto
  • 197
  • 3
  • 20
  • Did you try debugging it? Is the becomeFirstResponder getting called? – Frankenstein Jun 25 '20 at 18:42
  • it should be, if u see the code again, the part to remove the text field border is working. the borders are gone. this is using a reusable cell, so is that the case? – Aldo Sugiarto Jun 25 '20 at 18:48
  • I don't think it's getting called, you might be setting border somewhere else, if it was you should have faced with other issues because of it. Try using print statement right above becomeFirstResponder and see if it's printing out. If not check why it isn't, Where the execution is getting cut off. – Frankenstein Jun 25 '20 at 18:52
  • Make sure you call styling() from viewDidAppear and not from any other cycle event – Arik Segal Jun 25 '20 at 19:27
  • @AldoSugiarto Please check my answer – Nikunj Kumbhani Jun 26 '20 at 10:59

1 Answers1

0

You can do it with this

cell.textField.tag = indexpath.row
cell.textField.delegate = self

Handle in a delegate method

Nikunj Kumbhani
  • 3,758
  • 2
  • 26
  • 51