0

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?

Vimzy
  • 1,871
  • 8
  • 30
  • 56
  • I even tried reloading the table view, but it still doesn't work. – Vimzy Jun 05 '15 at 17:30
  • You have a debugger. Debug! Use logging or stepping. What is `searchController.searchBar.text`? What is `_array`? Are they the things you expect them to be? – matt Jun 05 '15 at 17:35
  • I agree with the above comment... use the debugger, or log statements if you like... check that there is something in _array to begin with (should be an array of strings given your predicate?) and that you filterUsingPredicate: is actually giving you back some items. – lateAtNight Jun 05 '15 at 20:19
  • Yes, I've tried all of that and they hold the correct information! – Vimzy Jun 05 '15 at 22:15

1 Answers1

0

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.

matt
  • 515,959
  • 87
  • 875
  • 1,141