iphonecocoa-touchuitableviewuisearchbarios-4.2

How can I force a UITableView to reloadData while keeping a UISearchBar active?


I've got a UITableView that has 20 sections. At the top I have a UISearchBar, and I want to filter the sections live as the user types.

Unfortunately, if the UISearchBar is active and if I return NO from searchBarShouldEndEditing: then my [tableView reloadData] call is ignored. If I return YES from searchBarShouldEndEditing: then the reloadData call works fine but I lose firstResponder after each character typed.

How can I force the UITableView to do live updates and filtering without having to resignFirstResponder on the UISearchBar between each character typed?


Solution

  • You could save yourself a lot of work by using the UISearchDisplayController and just feeding it the same datasource. It manages the search bar and its own table view for displaying filtered results.