4

The setup: The table has 1 custom cell. This cell has 1 text box. In each text box a number is entered. The tableView has 5 rows (of this cell). If I change one number in any of the cells, then all the cells need to be updated, I simply call tableView.reloadData(). The change in the text box is handled with the editingDidEnd event.

The problem: I click any of the textFields and change the number. Then, I click another textField to change its value. first editingDidEnd is called, all the values are re-calculated, and tableView.reloadData is called. Now, the click to the second textField is missed because the table is reloaded.

I am not able to update the cells directly because they are custom cells and the value is changed within that class. I cannot update ALL the cells from custom cell class.

A difficult question to explain in writing especially for a non native English speaker. If you understand the question, any pointers will be appreciated :).

Using Xcode 7.1 and Swift. Thank you.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
oyalhi
  • 3,834
  • 4
  • 40
  • 45

2 Answers2

4

When you call reloadData, all the cells are removed from the tableview and re-added. Since it is removed, it is no longer in the responder chain meaning its text field can't be the firstResponder (selected).


There are two options that could work.

Keep track of the row that is selected, call reloadData then call becomeFirstResponder on the text field for the correct row.

Not call reload data and just update the values in the text fields. This option more depends on the striation of your app.

Craig Siemens
  • 12,942
  • 1
  • 34
  • 51
  • If these are the two choices I have, then I'll go with the first one. The second option is not viable as the cells are all customs cell and they don't know about each other's state value etc. For the first option, I don't know if the cell even ever receives the `didSelectRowAtIndexPath` at all. Let me try and get back to you. Thank you for the suggestions. – oyalhi Nov 14 '15 at 19:15
  • I can call reloadData in the editingDidBegin event. Then it is not missed. I wish there was a better way to handle this. All I am doing is simply click from one textField to another. – oyalhi Nov 14 '15 at 19:24
  • The second option turned out to be much simpler to implement. – oyalhi Nov 14 '15 at 20:23
  • I have this same problem but even though I call becomeFirstResponder when the table is being created it doesn't actually select that textfield. What I have also tried is calling it after the command to reload the table but then what happens is the wrong textfield is called for some reason? – Nic Parmee Oct 19 '18 at 08:22
  • Calling `becomeFirstResponder` while the tableview is being created is probably too early. The textfield needs to be in the view hierarchy before that will work. You'll need to wait until `viewDidLayoutSubviews` or `tableView(_: willDisplay: forRow:)` called for something in a cell to become the first responder. – Craig Siemens Oct 19 '18 at 15:13
2

This can be quite simple, ideally you are in a position to store the values for the field you want to focus and the index path of the cell in question (such as when you regenerate the editing field after your reload?)

This code should help:

- (void)tableView:(UITableView *)tableView
  willDisplayCell:(UITableViewCell *)cell
forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath == self.editingIndexPath)
    {
        [self.editingTextField becomeFirstResponder];
    }
}
EricLeaf
  • 892
  • 5
  • 12