Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7604771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T23:59:46+00:00 2026-05-30T23:59:46+00:00

As the title says, in my code, when I search in my app happens

  • 0

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 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T23:59:47+00:00Added an answer on May 30, 2026 at 11:59 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

As the title says, will this code work and will it work in major
Like the title says, my code basically does this: set proxy, test proxy, do
As the title says, is there a way to remove code from a view
As the title says. Related to this question. Why does the following code not
Just like the title says, Is there a simple line of code I could
Pretty much what the title says really. We have some code that is .NET
Like the title says, how do you create custom code snippets in Visual Studio
Title says it mostly. I want to add a simple extension method to the
As the title says, I want to parse some Java source code in Java.
Like the title says, I have the following exception: Description: Event code: 3005 Event

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.