2
votes

I have implemented a SearchBar using an UIViewController, UISearchBarDelegate, UISearchDisplayDelegate, and when the search performs, my program send a http request to the server and then parser the response body. And this will cause the search bar delay for a few second after each character typed.

So I want to turn off the "live search" of the search bar so that it will not perform search every time I type a character. And also I want to perform search and display the data to the tableview when I click the "Search" button of the keyboard. What can I do?

1

1 Answers

1
votes

call [self.searchDisplayController.searchResultsTableView reloadData]; only when your search request has been completed and make the search request only when the delegate - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar is called.

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

    DLog(@"search text is %@",searchBar.text);

    [self makeSearchRequest:searchBar.text];
}

-(void)makeSearchRequest:(NSString*)pString
{
    CLLocation *location = [[AppHelper appDelegate] mLatestLocation];

    NSMutableDictionary *paramDic = [[NSMutableDictionary alloc] init];
    [paramDic setValue:[[AppHelper mDataManager] objectForKey:_KEY(KEY_USERID)] forKey:@"userid"];
    [paramDic setValue:@"H0001" forKey:@"client"];
    [paramDic setValue:[[AppHelper mDataManager] objectForKey:_KEY(KEY_LOCATION_ID)] forKey:@"locationid"];
    [paramDic setValue:@"0" forKey:@"pageid"];
    [paramDic setValue:@"10" forKey:@"displayrecords"];
    //  [paramDic setValue:[[dic objectForKey:@"birthday"] stringByReplacingOccurrencesOfString:@"/" withString:@"-"] forKey:@"dateofbirth"];
    **[paramDic setValue:pString forKey:@"keyword"];**




    [self.mWNetowrk makeRequsetWithURL:URL_SEARCH type:ReqSearch paramDictionary:paramDic delegate:self];
    [paramDic autorelease];
}

-(void)network:(WNetwork*)network didFinishLoadingWithRequest:(NSInteger)pReq data:(NSMutableDictionary*)pData
{
    [self removeLoader  ];

    switch (pReq) {

        case ReqSearch:
            self.mArrayPlaces = [pData objectForKey:@"places"];
            [mPlacesCacheArray release];
            mPlacesCacheArray=nil;
            if (!mPlacesCacheArray) {

                mPlacesCacheArray =[NSMutableArray new];
                for (int i =0 ; i<[mArrayPlaces count]; i++) {
                    [mPlacesCacheArray addObject:[NSNull null]];

                }
            }

            [self.searchDisplayController.searchResultsTableView reloadData];

            break;

        default:
            break;
    }

}