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/