Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableView reloadData automatically calls resignFirstResponder

I have this UITableView with custom cells that can get only predefined values, therefore I use a UIPickerView as their inputView. All is jolly good until I edit a field and need to show its updated value.

In order to make things clearer and easier to maintain, I made delegates and data sources as separate classes, and use notifications to make them interact with the tableView. So, after a value has been chosen from the UIPickerView, the tableView's data source gets notified, and in turn notifies the main ViewController that holds a reference to the tableView. From there I call

[_tableView reloadData];

and everything seems to work, except that the UIPickerView disappears, I think because the cells are regenerated and somewhere some resignFirstResponder is called, or something like that. Is there any other way to make the tableView updating its values without having to implement a custom method somewhere that does it, which would be quite ugly?

like image 441
Morpheu5 Avatar asked Jun 20 '11 09:06

Morpheu5


3 Answers

This reads like expected behavior - the picker belongs to a particular cell, that cell gets reloaded and is not the first responder any more. I guess one had to select a specific element anyway for the picker to appear, i.e. to make it first responder.

So you either need to make it become first responder again after reloading, or update the specific cell directly.

like image 63
Eiko Avatar answered Nov 08 '22 02:11

Eiko


adding:

[yourSearchBar becomeFirstResponder];

after your:

[_tableView reloadData];

did the trick

like image 22
valvoline Avatar answered Nov 08 '22 02:11

valvoline


I met the same problem, none of the answers above worked perfectly (I see the keyboard bouncing up and down, etc.).
Following this SO post I fixed the issue by calling

[tableView beginUpdates];
[tableView endUpdates]; 

this worked for me, table rows get updates and even expand/shrink (if you are changing rows height dynamically) with a nice animation, all without resigning first responder or even starting keyboard dismiss.
This will not scroll your table view to fit any expanded row, so I put the snippet above in dedicated method, f.e.:

- (void)tableView:(UITableView *)tableView reloadRowWhileShowingKeyboard:(NSIndexPath *)indexPath 
{  
    [tableView beginUpdates];
    [tableView endUpdates]; 

    [tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
like image 22
zutroi Avatar answered Nov 08 '22 04:11

zutroi