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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:40:19+00:00 2026-06-10T07:40:19+00:00

My app crashes if I do this: I make a search on my UITableView

  • 0

My app crashes if I do this:

  1. I make a search on my UITableView

  2. After a search I tap on UITableViewCell of searchDisplayController UITableView and go to another View Controller

  3. I then come back to the main UITableView, questionTable – the one in which I searched, and tap on the last cell, or any cell that is greater then the number of search results I previously had.

  4. App crashes with this error:

    *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 12 beyond bounds [0 .. 0]'
    *** First throw call stack:(0x35aa188f 0x37e48259 0x359ea9db 0x155fd 0x3384df5b 0x153ed 0x3358793d 0x33601627 0x355bb933 0x35a75a33 0x35a75699 0x35a7426f 0x359f74a5 0x359f736d 0x37693439 0x33503cd5 0x95a3 0x9548) terminate called throwing an exception(lldb) 
    

I suspect the problem is with my code, because searching UITableView is a new topic for me. That’s how I do it:

1.My VC conforms to these protocols <UITableViewDataSource, UITableViewDelegate, UISearchDisplayDelegate, UISearchBarDelegate>

2.I have these variables:

@property (nonatomic, retain) NSMutableArray *searchResults;
@property (nonatomic, copy) NSString *savedSearchTerm;

3. In viewDidLoad i set up the source of my UITableView which I search:

    self.questionList = [[NSArray alloc]
                     initWithObjects: MY OBJECTS HERE, nil];

if ([self savedSearchTerm])
{
    [[[self searchDisplayController] searchBar] setText:[self savedSearchTerm]];
}

4. I have this action to handle the search:

- (void)handleSearchForTerm:(NSString *)searchTerm
{
[self setSavedSearchTerm:searchTerm];

if ([self searchResults] == nil)
{
    NSMutableArray *array = [[NSMutableArray alloc] init];
    [self setSearchResults:array];
}
[[self searchResults] removeAllObjects];

if ([[self savedSearchTerm] length] != 0)
{
    for (NSString *currentString in [self questionList])
    {
        if ([currentString rangeOfString:searchTerm options:NSCaseInsensitiveSearch].location != NSNotFound)
        {
            [[self searchResults] addObject:currentString];
        }
    }
}   
}

5) Here’s how I set up my UITable views:

    (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    {
    NSInteger row = [indexPath row];
    NSString *contentForThisRow = nil;

    if (tableView == [[self searchDisplayController] searchResultsTableView])
        contentForThisRow = [[self searchResults] objectAtIndex:row];
    else
        contentForThisRow = [[self questionList] objectAtIndex:row];

    static NSString *CellIdentifier = @"questionsCell";

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

6) Here’s how my numberOfRowsInSection method looks like:

(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSInteger rows;

if (tableView == [[self searchDisplayController] searchResultsTableView])
rows = [[self searchResults] count];
else
rows = [[self questionList] count];
return rows;
}

7) Finally, I have these two methods:

(BOOL)searchDisplayController:(UISearchDisplayController *)controller 
shouldReloadTableForSearchString:(NSString *)searchString
{
    [self handleSearchForTerm:searchString];
    // Return YES to cause the search result table view to be reloaded.
    return YES;
}

(void)searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller
{
    [self setSavedSearchTerm:nil];
    [[self questionTable] reloadData];
}

I’m really sorry for the long question. I have made some mistake in the code and I hope someone could show me the right way to do this. The Search Bar Controller is properly linked in the IB
Any help would be highly appreciated!

NEW EDIT:
I’m pushing a new VC on didSelectRowAtIndePath:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self performSegueWithIdentifier:@"toAnswer" sender:indexPath];
}

And I’m updating an NSString there. It worked perfectly when I used just one TableView

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    ((QandADetailsViewController *)segue.destinationViewController).delegate=self;

    NSIndexPath *indexPath = (NSIndexPath *)sender;

    QandADetailsViewController *mdvc = segue.destinationViewController;

    if (self.searchResults == nil) {
        mdvc.questionName = [self.questionList
                             objectAtIndex:indexPath.row];
    } else {
        mdvc.questionName = [self.searchResults
                             objectAtIndex:indexPath.row];
    }
}
  • 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-06-10T07:40:21+00:00Added an answer on June 10, 2026 at 7:40 am

    In prepare for segue, the check for searchResults == nil is probably incorrect. There’s no reason to expect it to be nil ever after the first search. That means you’ll always be dereferencing the search array on subsequent table selections, explaining the index out of bounds.

    The fix, I think, is to ask if ([self.searchDisplayController isActive]) instead of whether search results exist.

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

Sidebar

Related Questions

my app crashes with this code.. it doesnt even start up.. any ideas guys
my app crashes often in this for-loop: for (int a = 0; a <=
my app crashes when the user tries to delete a row from the UITableView
I am using navigation controller to move from one view to another ... i
I've written an app with a custom Search class in it. This builds up
In my app i want to make array, add to toolbar and then release
How to make a callback after the view is completely rendered ? I am
I have a navigation based app. Press a button on main view, then I
I have a view controller initialized like this: HomeViewController *homeVC = [[HomeViewController alloc] initWithNibName:@HomeViewController
The App crashs when I add this line `requestWindowFeature(Window.FEATURE_NO_TITLE); may be the solution is

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.