Search code examples
iosobjective-cuisearchbaruisearchcontrolleruisearchbardelegate

UISearchController TableView Not Updating when Using Search bar to filter array


I'm using the search bar to grab the text the user inputs and then use that to filter through an NSArray, and then assign the newly constructed array to the specified class' array property. Code below:

-(void)updateSearchResultsForSearchController:(UISearchController *)searchController{

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF beginswith[c] %@",searchController.searchBar.text];

NSArray *temp = [_array filteredArrayUsingPredicate:predicate];

_resultViewController.dynamicArray = temp;


}

But my resultViewController's tableView isn't displaying any data. However, if I use the below, it works:

-(void)updateSearchResultsForSearchController:(UISearchController *)searchController{

NSArray *temp = [[NSArray alloc]initWithObjects:@"hello",@"bye",@"why",@"okay", nil];

_resultViewController.dynamicArray = temp;

}

I'm not quite sure how to fix this problem. It seems like when I assign _resultViewController's dynamic array to temp, temp hasn't been set yet when working with filtering the array. But it works fine in the second case, so I know the rest of the code is right.

Thoughts?


Solution

  • Without knowing more about your _resultViewController.dynamicArray, it is impossible to help. Things to think about:

    • Is _resultViewController the view controller you think it is - the actual view controller whose view is being displayed?

    • Why would merely setting _resultViewController.dynamicArray cause anything to happen in that view controller? For example, in my own code, the view controller in question is usually a UITableViewController - and so, after setting its model array, I always call the table view's reloadData so that we actually see the filtered data.