0
votes

As the title says, in my code, when I search in my app happens this in a simple way:

stations = [barcelona, madrid, valencia, badajoz]

when I type "ba" it shows:

mysearch = [barcelona, badajoz]

I want to select badajoz, so the program returns that row is position 1, when in stations is position number 3.

Any idea?

PD: Code related to search

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
    NSPredicate *resultPredicate = [NSPredicate 
                                    predicateWithFormat:@"SELF contains[cd] %@",
                                    searchText];

    self.searchResults = [self.allItems filteredArrayUsingPredicate:resultPredicate];
}

#pragma mark - UISearchDisplayController delegate methods
-(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;
}

EDIT: I edited my code using the answer below - (void)tableView: (UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ NSLog(@"Pushing..."); NSUInteger row = [indexPath row]; //older code before search view

    //index from the search
    id searchResult = [searchResults objectAtIndex:row];
    int indexForResult = [ListaEstaciones indexOfObject:searchResult];
    //more stuff

EDIT: Now it works for this cases: with the list with all elements it searches well, when I start to search it works well to, the problem is that when stop searching and I select a row of the list of all elements it still uses de index of the list of searched items before, this happens because in this code I change the searching boolean variable to YES:

    - (void)filterContentForSearchText:(NSString*)searchText 
                             scope:(NSString*)scope
{
    NSPredicate *resultPredicate = [NSPredicate 
                                    predicateWithFormat:@"SELF contains[cd] %@",
                                    searchText];

    self.searchResults = [self.allItems filteredArrayUsingPredicate:resultPredicate];
    searching = YES;

But I don't know where to change searching value to NO when I return to the list of all values. Any suggestions? Thanks a lot. EDIT 2: Solved with this website http://engineeringtheworld.wordpress.com/2011/04/11/detecting-when-clear-is-clicked-in-uisearchbar-x-button/

1

1 Answers

1
votes

If you have an object you can find out its index in an array. Using your example at the top:

id searchResult = [mysearch objectAtIndex:1];
int indexForResult = [stations indexOfObject:searchResult];

You won't want to process these two statements if you didn't do a search as they don't make sense. To stop them getting processed will require you put them inside an if statement and check if you have done a search or not.

After looking at your error it would seem that you have an array with 1160 Objects in it and your code is asking for object 2147483647. This obviously doesn't exist and so the code crashes. Why you get this error I'm not totally sure and is something only you can really answer. Use breakpoints and the debugger to find out where the code is going wrong.