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 7694029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T21:13:31+00:00 2026-05-31T21:13:31+00:00

I have a IUSearchBar with an UISearchDisplayController, and everything is working well: When I

  • 0

I have a IUSearchBar with an UISearchDisplayController, and everything is working well:

  • When I touch the search bar, the keyboard shows up, the TableView has a black cover on it and I can start typing

screenshot 1 http://joeranbosma.nl/xcode/sdc1.png

  • When I type something the clear button (x) appears, and so do my search results.

screenshot 1 http://joeranbosma.nl/xcode/sdc2.png

  • This works all great. When I click on the clear (x) button, it just returns to the first screenshot state (empty search bar, and a black box above the results)

But, here it comes!

  • When I type something and then move the search result, the keyboard hides (standard UISearchDisplayController thing)
    Like this:

screenshot 1 http://joeranbosma.nl/xcode/sdc3.png

When I then click on the clear (x) button, the program crashes with a SIGABRT error:

screenshot 1 http://joeranbosma.nl/xcode/sdc4.png

.. and I have no idea how to fix this 🙁

Here is my source code: (no idea what you need to solve the question)
http://joeranbosma.nl/xcode/wearch_stack.zip

But I think this is the most useful part:

- (void)viewDidLoad {
    [super viewDidLoad];

    //Initialize the array.
    listOfItems = [[NSMutableArray alloc] init];
    copyListOfItems = [[NSMutableArray alloc] init];
    staticlist = [[NSMutableArray alloc] init];

    staticlist = listOfItems;

    //Add items
    [listOfItems addObject:@"Iceland"];
    [listOfItems addObject:@"Greenland"];
    [listOfItems addObject:@"Switzerland"];
    [listOfItems addObject:@"Norway"];
    [listOfItems addObject:@"New Zealand"];
    [listOfItems addObject:@"Greece"];
    [listOfItems addObject:@"Rome"];
    [listOfItems addObject:@"Ireland"];

    //Set the title
    self.navigationItem.title = @"Wearch";

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
// Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
   return 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (searching)
        return [copyListOfItems count];
    else {
        return [listOfItems count];
    }
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    // Configure the cell.
    if(searching)
        cell.textLabel.text = [copyListOfItems objectAtIndex:indexPath.row];
    else {
        NSString *txtLbl = [listOfItems objectAtIndex:indexPath.row];
        cell.textLabel.text = [txtLbl stringByAppendingString:@"x"];
    }

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    //Get the selected word

    NSString *selectedWord = nil;

    if(searching)
        selectedWord = [copyListOfItems objectAtIndex:indexPath.row];
    else {
        selectedWord = [listOfItems objectAtIndex:indexPath.row];
    }

    //Initialize the detail view controller and display it.
    DetailViewController *dvController = [[DetailViewController alloc] initWithNibName:@"DetailView" bundle:[NSBundle mainBundle]];
    dvController.selectedWord = selectedWord;
    [self.navigationController pushViewController:dvController animated:YES];
    [dvController release];
    dvController = nil;  
}
- (void) searchBarTextDidBeginEditing:(UISearchBar *)theSearchBar {
    searching = YES;
}
- (void)searchBar:(UISearchBar *)theSearchBar textDidChange:(NSString *)searchText {
    //Remove all objects first.
    [copyListOfItems removeAllObjects];

    if([searchText length] > 0) {
        searching = YES;
        [self searchTableView];
    }
    else {
        searching = NO;
    }

    [self.tableView reloadData];
}
- (void) searchBarSearchButtonClicked:(UISearchBar *)theSearchBar {
    [self searchTableView];
}
- (void) searchTableView {

    NSString *searchText = searchBar.text;
    NSMutableArray *searchArray = [[NSMutableArray alloc] init];

    NSMutableArray *results1 = [[NSMutableArray alloc] init];
    NSMutableArray *results2 = [[NSMutableArray alloc] init];

    [searchArray addObjectsFromArray:listOfItems];

    for (NSString *sTemp in searchArray) {
        NSRange titleResultsRange = [sTemp rangeOfString:searchText options:NSCaseInsensitiveSearch];

        if (titleResultsRange.length > 0){
            if (titleResultsRange.location == 0) {
                [results1 addObject:sTemp];
            }
            else{
                [results2 addObject:sTemp];
            }
        }
    }

    for (NSString *sTemp in results1) {
        [copyListOfItems addObject:sTemp];
    }
    for (NSString *sTemp in results2) {
        [copyListOfItems addObject:sTemp];
    }

    [searchArray release];
    searchArray = nil;
}

I hope one of you can solve this.

Can you also say if you like the images?

  • 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-31T21:13:33+00:00Added an answer on May 31, 2026 at 9:13 pm

    This is an interesting one. The long answer is that searchBarTextDidBeginEditing: is being called after tableView:numberOfRowsInSection: but before tableView:cellForRowAtIndexPath:. The result is that the tableView is expecting [listOfItems count] number of rows but by the time it calls tableView:cellForRowAtIndexPath: the searching BOOL is YES so the view controller uses copyListOfItems to populate the table which of course does not contain a sufficient number of objects to populate the table.

    The short answer is, stop telling the view controller you are searching until you are searching; Set searching = YES; when there is actual text you are using to search not just when the searchBar is first responder.

    The really short answer:

    - (void) searchBarTextDidBeginEditing:(UISearchBar *)theSearchBar {
        // Comment this assignment out
        //searching = YES;
    }
    

    It seems to work fine.

    A couple of tips for such a scenario:

    1) Look at the printed stacktrace in your console. It would have let you see that the problem was:

    'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'
    

    2) Use NSLogs profusely; I find the most helpful to be:

    NSLog(@"%@",NSStringFromSelector(_cmd));
    

    Edit In response to the non-symbolicated stack-trace in the console(i.e. 0x14d3ec9 0x36e5c2):

    3) Use an exception breakpoint; as pictured:

    enter image description here

    4) Add an uncaught exception handler; As described here.

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

Sidebar

Related Questions

I have a UISearchBar and UISearchDisplayController, everything works great but my scope selector displays
I have implemented a UISearchBar into a table view and almost everything is working
I have a little issue here. Why does my search bar search when I
I have the search bar in the top of the table and the hidden
Have a matrix report now that has Position, Hours and Wages for a location
I'm trying to create a UISearchDisplayController programmatically. I have a method which should set
I have a search functionality using UISearchBar that occurs on-the-fly, so I think it
I have a UISearchBar on a bottom toolbar. The problem is the keyboard covers
I have a UISearchDisplayController and UISearchBar hooked up to my ViewController via Outlets from
I want to close my UISearchDisplayController when the user clicks the Search button since

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.