So I'm not sure if I set this up correctly. I have a SearchDisplayController
and search bar.
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 150, 44)];
self.SearchEntry = searchBar;
self.SearchEntry.tintColor = DARKGRAY_COLOR;
self.SearchEntry.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
self.SearchEntry.delegate = self;
UISearchDisplayController *searchDisplayCtlr = [[UISearchDisplayController alloc] initWithSearchBar:_searchEntry contentsController:self];
self.SearchController = searchDisplayCtlr;
These go inside a UIToolbar
. Because querying the database for certain values can take awhile, I took code out and put it in an NSOperation
subclass. At the end, it calls back the ViewController through a delegate to update the actual data:
[self.searchOperationDelegate searchDidFinishWithResults:self.searchResults];
So when actual typing goes in my search bar, I do this:
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
if (_queue.operationCount > 0) {
[_queue cancelAllOperations];
}
SearchOperation *search = [[SearchOperation alloc] initWithSearchText:searchText];
search.searchOperationDelegate = self;
[self.queue addOperation:search];
}
I am basically canceling any previous search and only searching on what is currently in the searchText
string.
And then in my delegate callback method in my ViewController
- (void)searchDidFinishWithResults:(NSArray *)results {
NSLog(@"resultsList: %i", [results count]);
[self performSelectorOnMainThread:@selector(setResultsList:) withObject:results waitUntilDone:YES];
// [self.searchDisplayController.searchResultsTableView reloadData];
}
I also have a log in my cellForRowAtIndexPath
method to check how many items are in my [results count]. When I look, I basically get cellForRowAtIndexPath
called many times (say 1000-3000 based on what we are searching for), but then in my searchDidFinishWithResults:
method, I get 1 item. It's always cellForRowAtIndexPath
that gets called, and then this searchDidFinishWithResults:
method. So after I update the model, the table doesn't update again and I'm not sure why. I thought I could manually call reloadData
, but that gives me the same results.
So a more concrete example of what get's logged is this:
resultsList: 2909 (I type in one key, and only this gets logged)
tableView:cellForRowAtIndexPath:] 2909 (my second key in the search bar, this gets logged)
tableView:cellForRowAtIndexPath:] 2909
tableView:cellForRowAtIndexPath:] 2909
tableView:cellForRowAtIndexPath:] 2909
tableView:cellForRowAtIndexPath:] 2909
tableView:cellForRowAtIndexPath:] 2909
tableView:cellForRowAtIndexPath:] 2909
resultsList: 1370 (this is the last thing that gets logged when my 2nd key is pressed)
tableView:cellForRowAtIndexPath:] 1370 (this is the first thing that gets logged when my 3rd key is pressed)
tableView:cellForRowAtIndexPath:] 1370
tableView:cellForRowAtIndexPath:] 1370
tableView:cellForRowAtIndexPath:] 1370
tableView:cellForRowAtIndexPath:] 1370
tableView:cellForRowAtIndexPath:] 1370
tableView:cellForRowAtIndexPath:] 1370
resultsList: 1 (last thing that gets logged when my 3rd key is pressed)
Any thoughts? Thanks in advance!