Prastow
Prastow

Reputation: 178

search method not working after second hit

I've a problem with my UISearchDisplayController, the search is not working properly.

This is my code:

    - (void)filterContentForSearchText:(NSString*)searchText 
                             scope:(NSString*)scope
{
    [self.searchResults removeAllObjects];

    for (int i = 0; i < [temp_category count]; i++) {
        BOOL foundResult = FALSE;

        if ([[temp_category objectAtIndex:i] rangeOfString:searchText].location != NSNotFound) {
            foundResult = TRUE;
        }
        if ([[price_producttitle objectAtIndex:i] rangeOfString:searchText].location != NSNotFound) {
            foundResult = TRUE;
        }
        if ([[price_type objectAtIndex:i] rangeOfString:searchText].location != NSNotFound) {
            foundResult = TRUE;
        }
        if ([[price_description objectAtIndex:i] rangeOfString:searchText].location != NSNotFound) {
            foundResult = TRUE;
        }
        if (foundResult) {

            NSNumber *result = [NSNumber numberWithInt:i];
            if ([self searchResults] == nil) {
                NSMutableArray *array = [[NSMutableArray alloc] init];
                [self setSearchResults:array];
                [array release];
            }

                [searchResults addObject:result];

        }
    }

    NSLog (@"array = %i", [searchResults count]);
    NSLog(@"%@",searchResults);
}

    -(BOOL)searchDisplayController:(UISearchDisplayController *)controller 
shouldReloadTableForSearchString:(NSString *)searchString
{

    [self filterContentForSearchText:searchString 
                               scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
                                      objectAtIndex:[self.searchDisplayController.searchBar
                                                     selectedScopeButtonIndex]]]; 

    return YES;

}

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller 
shouldReloadTableForSearchScope:(NSInteger)searchOption
{
    [self filterContentForSearchText:[self.searchDisplayController.searchBar text] 
                               scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
                                      objectAtIndex:searchOption]]; 

    return YES;
}

But I'm still confused, because when I start a search with the first letter, it gives the correct hits. But when I enter the second letter, it only shows one result (while there are more, as far as I know from my data sample). I'm doing something incorrectly. I think it has something to do with when the user enters text, but I'm confused which method I should use.

The code I now have is a combination of: this tutorial and this SO question.

Can someone give me a hint in the good direction? Displaying the results is fine, only this aspect bothers me. I think it has something to do with firing the method and [self.searchResults removeAllObjects];.

Upvotes: 1

Views: 202

Answers (1)

Prastow
Prastow

Reputation: 178

I would like to add my code, but the thing is that I still use the code I have above, but I'm manually implementing the UISearchBar (which I found somewhere else in a tutorial) instead of using SearchDisplayController. I also had difficulties with the navigationbar which dissappears when using SearchDisplayController, which gave me enough reason to implement it myself instead of using SearchDisplayController. It gives you more freedom.

At first it seemed a lot of work, so I choose to use SearchDisplayController, but I really advise anyone who needs some modification, or who wants more freedom, please do it manually with UISearchBar and a UITableView :)

Upvotes: 1

Related Questions