iosobjective-ciosdeployment

UITableView updating results from UISearchBar


I have a ViewController with a UISearchBar and UITablevView. The tableview just displays a list of items in the array, but I would like for it to display the filtered list of items with filteredArrayUsingPredicate. I'm able to get the filtered array using the predicate NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[cd] %@ OR SELF LIKE[cd] %@", searchText,searchText]; and when I print the count of the filtered array, the number of items is correct. But then when I try to display it in the table a crash occurs with this error: *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndexedSubscript:]: index 3 beyond bounds [0 .. 2]'

Here's the code for the search bar and populating the table:

@interface ViewController (){
    NSMutableArray<NSString*> *_items;
    NSMutableArray<NSString*> *_filtered;
    bool _searching;
}


@end

@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self->_items = @[@"iphone",@"ipad",@"ipod",@"imac"];
    self->_filtered = [[NSMutableArray alloc] initWithArray:self->_items];
    self->_table.dataSource = self;
    [self->_table setDelegate:self];
    [self->_search setDelegate:self];
    self->_searching = false;

}
- (void)searchBar:(UISearchBar *)searchBar
    textDidChange:(NSString *)searchText{
    self->_searching = true;
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[cd] %@ OR SELF LIKE[cd] %@", searchText,searchText];
    self->_filtered = [self->_items filteredArrayUsingPredicate:predicate];
    NSLog(@"%lu",(unsigned long)[self->_filtered count]);

    [self->_table reloadData];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
   return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self->_items.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    if(self->_searching)
        cell.textLabel.text = self->_filtered[indexPath.row];
    else
        cell.textLabel.text = self->_items[indexPath.row];
    return cell;
}
@end

The line that seems to be causing the crash is this [self->_table reloadData]; in - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText In that method I set searching to true and in my cellForRowAtIndexPath I check if searching is true then display filtered results otherwise display items. But I don't understand why a crash occurs if I reload data and tell it to display the filtered results.


Solution

  • You're returning the unfiltered item count from numberOfRowsInSection when you should be returning the filtered item count if _searching is true.

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        if (_searching) {
            return _filtered.count;
        }
    
        return _items.count;
    }