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.
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.
Thanks eyebrowsoffire, you showed me right way 🙂