0

I trying to do an online search using searchBar, I doing next:

//-=-=-=-=Methods for search Bar=-=-=-=
-(void)searchThroughData {

    self.result=nil;

    //Send searched substrint ti server
    if (self.searchBar.text.length > 0) {

        NSString *searchParams = [NSString stringWithFormat:@"<request<user_name>%@</user_name></request>", self.searchBar.text];

        [self initRequest:searchParams];
    }
}


-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
    [self searchThroughData];
}
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=


- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

    NSString *dataString = [[NSString alloc] initWithData:_receivedData encoding:NSUTF8StringEncoding];

    // -=-=-=-=-=-=-=-=-=-=Parse the XML into a dictionary-=-=-=-=-=-=-=-=-=-=
    NSError *parseError = nil;
    _xmlDictionary= [XMLReader dictionaryForXMLString:dataString error:&parseError];

    NSDictionary * dict=[_xmlDictionary objectForKey:@"result"];
    NSDictionary *dict1 = [dict valueForKey:@"user"];

    _result = [[NSMutableArray alloc] initWithArray:[[dict1 valueForKey:@"name"] valueForKey:@"text"]];

      NSLog(@"res: %@", _result);

       [self.tableView reloadData];

}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    /*        !!!This never get called!!!
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        NSLog(@"in search");
    }
     */

     if(!self.searchDisplayController.isActive) {
        return myCategories.count;
    }
    else
    {
         NSLog(@"in search === %d", _result.count);
        return _result.count;
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];

    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
    }


    if (!self.searchDisplayController.isActive) {
        cell.textLabel.text = myCategories[indexPath.row];
        return cell;
    }
    else {
        NSLog(@"search cllFrRwAt %@", _result[indexPath.row]);
        cell.textLabel.text = _result[indexPath.row];
        return cell;
    }
}

I can see in console that "in search === 5", and "search cllFrRwAt john" (5 times), but on the screen I see "no result". Only if I tap Cancel button on searchBar I'm see my downloaded result. How can I show download result immediatly as data was downloaded and update searchDisplay?

EDIT:

I've changed searchThroughData method with :

-(void)searchThroughDataWithString: (NSString *)searchingStr {

…

}

and

[[NSOperationQueue mainQueue] addOperationWithBlock:^ {

                NSLog(@"Main Thread Code");
                [self.tableView reloadData];

            }];

But this doesnt help

daleijn
  • 718
  • 13
  • 22
  • First thing you should do is pass the `searchText` from searchBar:textDidChange: to your search method. Second thing is that your search process is fairly expensive (request and parsing), and there's no guarantee searches will complete in the order they are initiated. Third, you should make sure `reloadData` is always called on the main thread. – Farski Apr 23 '14 at 20:03
  • But I can see downloaded array everytime in the console, I can't see it on the seachDisplayController – daleijn Apr 23 '14 at 20:27
  • You're doing all the different parts of this process a little strangely. In your delegate and data source methods you should be checking to see if the tableView being passed in is the tableView of the search controller. – Farski Apr 24 '14 at 19:02
  • You also want to make sure you're calling `reloadData` on the search results table; self.tableView usually points to the view controller's own tableView. – Farski Apr 24 '14 at 19:03

0 Answers0