user3181152
user3181152

Reputation: 25

How should I reload searchResults table view, when I use HTTP API for searching?

I have this methods, and try to refresh this table view, when i have a response from server, but results Table don't reload.

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
    [self filterContentForSearchText:searchString
                               scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
                                      objectAtIndex:[self.searchDisplayController.searchBar
                                                     selectedScopeButtonIndex]]];
    return NO;
}

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
    NSMutableString *webString = [[NSMutableString alloc] initWithFormat: @"http://dsadda.ru/common/actions/catalog.php?scode=kLg2sij45uJhj&text=%@", searchText];
    webString = [[webString stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding] mutableCopy];

    NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
    NSURLSession *session = [NSURLSession sessionWithConfiguration: config];
    NSURLSessionTask *task = [session dataTaskWithURL: [NSURL URLWithString: webString] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
                              {
                                  NSDictionary *answer = [NSJSONSerialization JSONObjectWithData: data options: 0 error: nil];

                                  firmsList = [[NSMutableArray alloc] initWithArray:[answer objectForKey:@"FirmsList"]];

                                  [[_searchController searchResultsTableView] reloadData];
                              }];
    [task resume];
}

So please tell me the way, how to reload this tableView, when i recieve the response from server.

Upvotes: 0

Views: 216

Answers (2)

user3181152
user3181152

Reputation: 25

The problem was so silly. I forget that NSURLConnection working in background, and i should use main thread to reload table view.

Upvotes: 1

Mike Simz
Mike Simz

Reputation: 4026

Whenever you receive the response from the server, at the end of the completion handler just put [yourtableview reloadData].

If that isn't working for you, then it is most likely an issue with your table view delegate methods.

Check these delegate methods:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

Make sure that your 'firms list' array is embedded into these delegate methods so that when you do call [yourtableview reloadData], the newly filled 'firms list' array is what the table is pulling the data from.

Upvotes: 0

Related Questions