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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:53:20+00:00 2026-06-14T15:53:20+00:00

I’m currently trying to implement searching by following this tutorial. http://www.appcoda.com/how-to-add-search-bar-uitableview/ However, I’m at

  • 0

I’m currently trying to implement searching by following this tutorial.
http://www.appcoda.com/how-to-add-search-bar-uitableview/
However, I’m at the point where my app crashes, but I’ve got no idea, how to fix it.

JSON that comes to my app, has currently data for only one row (and I can’t change it, since I don’t have db access).

My initial view looks good. TableView is populated (with 1 row). When I tap on search bar and put in some characters that aren’t in that item’s name, I get normal “no result found” screen.
When I put in any character, that is present in item’s name, it shows me filtered item as expected. However, when I put in second letter, that is in item’s name, my app crashes, giving following output in console.

Terminating app due to uncaught exception 'NSRangeException', reason: '-[__NSCFArray objectAtIndex:]: index (1) beyond bounds (1)'

Now to my code.
numberOfRowsInSection method is modified depending on searching.

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        return [searchResults count];


    } else {
        return [[resultSearch valueForKeyPath:@"data.auctions"]count];
        }
}

numberOfSectionsInTableView has just one section hard coded (return 1; as I don’t need currently more)

Now my cellForRowAtIndexPath:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {

        static NSString *simpleTableIdentifier = @"SimpleTableItem";
        SimpleTableCell *cell = (SimpleTableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; //custom cell

        if (cell == nil)
        {
            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SimpleTableCell" owner:self options:nil];
            cell = [nib objectAtIndex:0];

        }


        NSDictionary *aucdict = [jsonAukResultsSearch objectAtIndex:indexPath.row]; //assigning current row from NSMutableArray(jsonAukResultsSearch) to Dictionary, this is probably crash spot
        NSURL *imageURL = [NSURL URLWithString:[aucdict objectForKey:@"img_url"]];
        NSData *result = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
//sending request to get picture
        UIImage *imageLoad = [[UIImage alloc] initWithData:result];
        NSString *timeString = [aucdict objectForKey:@"time_left"];
        NSString *priceString = [aucdict objectForKey:@"current_value"];


        // Configure the cell...
        if (tableView == self.searchDisplayController.searchResultsTableView) {
            cell.textLabel.text = [searchResults objectAtIndex:indexPath.row];
        } else {
            cell.nameLabel.text = [aucdict objectForKey:@"name"];
        }

        //cell.thumbnailImageView.image = imageLoad;
        //commented while crashes are happening


        return cell;
    }

Now, after my app crashes, my line with

NSDictionary *aucdict = [jsonAukResultsSearch objectAtIndex:indexPath.row];

is highlighter, so I guess, that is probably place, where indexPath parameter is out of bounds. I really don’t understand how can index(1) be beyond bounds(1). I know, that I have only zero or one returning rows (depending on search phrase).

I just started working with searching today, so I don’t have any clue, what those tricky spots are.. Any suggestions are appreciated 🙂 Thank you, Yanchi

Edit: Ok so I found where my problem may be… I’m closely follow that tutorial mentioned in top of this question.. This method is supposed to return array with search results.

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{

    NSPredicate *resultPredicate = [NSPredicate
                                    predicateWithFormat:@"SELF contains[cd] %@",
                                    searchText];

    searchResults = [poleMien filteredArrayUsingPredicate:resultPredicate];
    NSLog(@"%u",[searchResults count]);
}

I added NSLog as you can see… Now when I add first letter, that matches, console output is 1 (as it should be) so my filtered results will display without problem. However, when I enter another matching letter, my searchResults array will increase to 2 (as if it would count “matching” letters) and thats probably reason, why I get crash (trying to access index 1, while I have data for only one item in my searchResults array)

After that I changed my NSLog to display content of array… After inserting matching letters, same item gets added into my array (so i have another item in my array, that has same data as first item in my array)

EDIT 2: Ok so my problem is, that whenever I’m saving json data from dictionary to array (so that I can use array for filtering results) I do it in cellForRowAtIndexPath.. So my array gets same value loaded each time, search table is “repopulated”.. Now I need to find some way to load data into array only if it is different item.

  • 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-14T15:53:21+00:00Added an answer on June 14, 2026 at 3:53 pm

    Ok finally I found out what was wrong. I was saving name field of json response into array. My array was being updated in cellForRowAtIndexPath, so after entering two letters in searchbox, that matched my result, that result was saved into array two times (so I had 2 same items in my array) therefore I had crash because index was out of bounds.

    I repaired it by checking if my array contains same values and saving unique values into another array like this.

    newarray = [[NSMutableArray alloc] init];
        NSString *laststring = nil;
        for (NSString *currentstring in poleMien)
        {
            if (![currentstring isEqualToString:laststring]) [newarray addObject:currentstring];
            laststring = currentstring;
        }
    

    Thanks eyebrowsoffire, you showed me right way 🙂

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

Sidebar

Related Questions

I'm not entirely sure how I managed to jack this up. http://pretty-senshi.com If you
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text

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.