Question

I have implemented a UISearchBar in my app that is crashing when I hit the clear button and while there's one item found. It's like I type in some character, the app searches and display 1 item, but I when I clear the search, it crashes.

I searched stackoverflow and I am aware that the problem is with the array that holds the search items cause when I hit the clear button the array becomes empty and the tableview index path.row can't be displayed.

Also, if the search returns no item and I hit the clear button I get a similar error message buy this time saying the I got an empty array.

The app stops here:

NSDictionary *itemAtIndice =(NSDictionary *)[filteredCodigos objectAtIndex:indexPath.row];

The problem is that I simple don't know what to do!!!

Here is the error:

 Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]'

And here is my code::

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
if (searchText.length == 0) {
    isFiltered = NO;
} else {
    isFiltered = YES;
    filteredCodigos = [[NSMutableArray alloc]init];


    for (NSDictionary *item in values) {
        NSString *string = [item objectForKey:@"NAME"];

        NSRange stringRange = [string rangeOfString:searchText options:NSCaseInsensitiveSearch];

        if (stringRange.location != NSNotFound) {
            [filteredCodigos addObject:item];
        }
    }
}
[self.tableView reloadData];
}


- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    [self.Pesquisa resignFirstResponder];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (isFiltered) {
        return [filteredCodigos count];

    }

    return values.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath    *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"   forIndexPath:indexPath];

NSDictionary *itemAtIndex =(NSDictionary *)[values objectAtIndex:indexPath.row];
NSDictionary *itemAtIndice =(NSDictionary *)[filteredCodigos objectAtIndex:indexPath.row];

if (!isFiltered) {
    cell.textLabel.text = [itemAtIndex objectForKey:@"SYMBOL"];
    cell.detailTextLabel.font = [UIFont systemFontOfSize:10];
    cell.detailTextLabel.text = [itemAtIndex objectForKey:@"NAME"];
} else {
        cell.textLabel.text = [itemAtIndice objectForKey:@"SYMBOL"];
        cell.detailTextLabel.font = [UIFont systemFontOfSize:10];
        cell.detailTextLabel.text = [itemAtIndice objectForKey:@"NAME"];
       }
return cell;
}
Was it helpful?

Solution 2

Finally managed to fix this issue.

I just replaced indexPath.row for lastObject at the line that was giving me the error:

So, before:

NSDictionary *itemAtIndice =(NSDictionary *)[filteredCodigos objectAtIndex:indexPath.row];

And after:

NSDictionary *itemAtIndice =(NSDictionary *)[filteredCodigos lastObject];

OTHER TIPS

filteredCodigos is declared outside of searchBar:textDidChange: and is used in multiple places but is assigned to in searchBar:textDidChange:. Most likely, some other code is trying to access members of filteredCodigos which is unassigned or empty. Enabling exception breakpoints will help you track it down.

As a side note. You might look at NSPredicate to filter your items instead of the for loop.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top