SearchBar not displaying results until cancel button clicked

﹥>﹥吖頭↗ 提交于 2019-12-09 22:41:36

问题


I have implemented a UITableView with search bar (and search display) - all works fine, but the table results do not get updated until the search bar cancel button is tapped.

Delegate methods:

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    // asynchronous request with [self.tableView reloadData] in the connectionDidFinishLoading
    [self getProductData:searchBar.text]; 
 [searchBar resignFirstResponder];
 [self.tableView reloadData];


}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {

}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {

}

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {

}

- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar  {  
} 

- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar {
 return YES;
}

Do I need to call a delegate method after receiving the data from the server? Or should I make the request synchronous?

Thanks


edit: I tried with a synchronous request and it still does not work!


回答1:


Resolved this issue by adding this code to the end of my getProductData method:

[[[self searchDisplayController] searchResultsTableView] performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];



回答2:


I think you have to implement:

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString

and return YES

See the TableSearch sample code




回答3:


Please try something like this:

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
    [self getProductData:searchText]; 
    [self.tableView reloadData];
}


来源:https://stackoverflow.com/questions/4042111/searchbar-not-displaying-results-until-cancel-button-clicked

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!