Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UISearchBar not working when enter the first character?

I Would Implemented SearchBar Concept in my app but it have a bug, when i enter the first character in search bar it is not working and tableview not shows the related fields and it shows empty tableview but i enter the second character it displays the related fields in tableview, i don't know what mistake was made me please help me.

Here I give my code.

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
       NSLog(@"searching::");
       if(searchBar.text.length == 0)
       {
            [searchDict removeAllObjects];
            [arrayDict removeAllObjects];
            searchDict=[[NSMutableArray alloc]init];
            arrayDict=[[NSMutableArray alloc]init];
            [self getJsonResponse];
            [self.tableView reloadData];    
       } else {
            [arrayDict removeAllObjects];
            arrayDict=[[NSMutableArray alloc]init];
       }
       dispatch_async(dispatch_get_main_queue(), ^{
       [self getSearchJsonResponse:searchBar.text];
       NSLog(@"searchDict::%@",searchDict);
       [self.tableView reloadData];
   });    
}
like image 475
B.Saravana Kumar Avatar asked Dec 09 '15 10:12

B.Saravana Kumar


1 Answers

Based on your code from the chat, you need to thoroughly check your code for memory leaks and performance problems. It is littered all over the place.

That put aside, make the following changes to give your code a chance to work:

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
  if(searchBar.text.length == 0) {
    [searchDict removeAllObjects];
    [arrayDict removeAllObjects];
    [self getJsonResponse];
    } 
  else {
    [arrayDict removeAllObjects];
    [self getSearchJsonResponse:searchBar.text];
    }
}

Also, wrap [self.tableView reloadData] in a dispatch to the main queue in your web services' completion blocks like so:

dispatch_async(dispatch_get_main_queue(), ^{
   [self.tableView reloadData];
});

Additionally, what does NSLog(@"SEARCHDICT::%@",searchDict); log? Could it be possible that your lack of errorhandling is swallowing up a failed web service call response (like, empty search bar or 'I'm not searching for length 1')?

like image 174
Tobi Nary Avatar answered Oct 20 '22 15:10

Tobi Nary