AR89
AR89

Reputation: 3638

textFieldDidEndEditing vs didSelectRowAtIndexPath, the later not being called

I have a view with a UITextField and a UITableView. While the user is entering text I want the didSelectRowAtIndexPath to be called, unfortunately it isn't. It seems that the focus of the textfield prevents the didSelectRowAtIndexPath to be called.

The first solution I found would be to create a gesture recognizer and check if the event coordinates are inside the table, and then check in which row. But I would like to make a simpler solution, I'm probably missing something.

Edit: the textfield is not inside the cell, is outside the table, you are suggesting answers as if it is inside the cell.

Upvotes: 0

Views: 399

Answers (3)

Chirag Patel
Chirag Patel

Reputation: 1481

Write code inside cellForRowAIndexPath method of uitableview.that prevent from didSelectForRowAtIndexPath cslled method.

tableview.selectiostyle = UItableviewSelectionStayle.None

Upvotes: -1

Shades
Shades

Reputation: 5616

When textFieldDidBeginEditing is called, you can get the cell row, then use it however you need to:

func textFieldDidBeginEditing(textField: UITextField) {
    let currentCell = textField.superview!.superview! // Get the cell
    let indexPath = tableView.indexPathForCell(currentCell)
    let currentRow = (indexPath?.row)! // Get the row of that cell
}

Upvotes: 2

Ross Barbish
Ross Barbish

Reputation: 679

In cellForRow, set the tag of the UITextField to the indexPath.row. Now, when your textfield begins editing, you'll have the row of the indexpath, and can fetch the cellForRow if needed. Put your "didSelectRowAtIndexPath" function logic in a separate function and call that new function from both didSelect and beginsEditing.

Upvotes: 0

Related Questions