Search code examples
objective-cuisearchdisplaycontroller

Objective C UISearchBar


I have a little issue here. Why does my search bar search when I start inputting some text? Here's the code:

     - (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
         [searchBar setShowsCancelButton:YES animated:YES];
         self.theTableView.allowsSelection = NO;
         self.theTableView.scrollEnabled = NO;
         [theTableView setRowHeight:110];
     }

     - (void)searchDisplayController:(UISearchDisplayController *)controller willShowSearchResultsTableView:(UITableView *)tableView {
         [tableView setRowHeight:[[self theTableView] rowHeight]];
         tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
         [self.tableData removeAllObjects];
     }

     - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
         searchBar.text=@"";

         [searchBar setShowsCancelButton:NO animated:YES];
         [searchBar resignFirstResponder];
         self.theTableView.allowsSelection = YES;
         self.theTableView.scrollEnabled = YES;
     }

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

       // all the function to search

     }

Solution

  • Implement searchDisplayController:shouldReloadTableForSearchString: and return NO. This should prevent the autoreloading of the data while you search.