Question

I have UITextField inside the first cell of UITableView, whenever it changes the table is changing too. The problem is that when the table is updated the field loses first responder status and becomeFirstResponder doesn't make it first responder again - the keyboard disappears.

I call [_searchField becomeFirstResponder] in tableView:didEndDisplayingCell:forRowAtIndexPath: but keyboard disappears anyway. Update: I tried calling becomeFirstResponder in cellForRowAtIndexPath before, it didn't work too. _searchField is an outlet property of the class of cell connected to the field.

Where should I call becomeFirstResponder so that field becomes first responder? Or maybe I should do it somehow differently so that field doesn't lose first responder status? I tried refreshing not the whole table, but separate sections (search field is in its own section), but it didn't work, the controller tried to show cells that didn't exist anymore, so I would prefer refreshing the whole table.

I know that I can use special search controller, but I didn't like its design, so I implemented it this way...

Thanks!

Was it helpful?

Solution

Actually, I should've simply called becomeFirstResponder AFTER [self.tableView reloadData] in the same method, instead of trying to find some method that was called after table refresh inside UITableViewCell subclass or in UITableViewController class.

I didn't do it in the first place, probably, as I didn't have the reference to the cell in the place where [self.tableView reloadData] was called.

That's what worked (SOPSearchCell is the subclass of UITableViewCell, self is UITableViewController subclass instance):

[self.tableView reloadData]
// make searchField first responder again
NSUInteger indexes[] = {0, 0};
NSIndexPath *indexPath = [NSIndexPath indexPathWithIndexes:indexes length:2];
SOPSearchCell *cell = (SOPSearchCell *)[self.tableView cellForRowAtIndexPath: indexPath];
[cell.searchField becomeFirstResponder];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top