Reputation: 2649
I found this perfect answer to search through an NSFetchedResultsController : https://stackoverflow.com/a/4481896/1486928
EDIT : project showing the issue : http://cl.ly/2x0C0N0E4240
It seems really great except it wasn't written to use with ARC, I tried to just remove all the "retain" "release" "autorelease".
It still works, well mostly, the thing is, when I enter a char in the searchbar it shows the filtered table as expected but it only takes 1 char (if you add more it doesn't do anything) and after that every other "search" will show the results of the first search that only took 1 char.
I've been at it for 2 days putting NSlog anywhere to see when every methods are called but still couldn't find how to make it work :(
Thanks !
Edit : here is .m http://pastebin.com/9U4TfbA6 Edit : here is .h http://pastebin.com/S9aaNRFE
Also if it can help the search works when I comment this :
- (NSFetchedResultsController *)fetchedResultsController {
if (fetchedResultsController_! = nil)
{
return fetchedResultsController_;
}
...
}
And this :
- (NSFetchedResultsController *)searchFetchedResultsController {
if (searchFetchedResultsController_ != nil)
{
return searchFetchedResultsController_;
}
...
}
But it mess up other things :/
Upvotes: 3
Views: 1203
Reputation: 4946
I guess that you are messing up with the search display controller delegate methods,
and especially you need to check this method
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString;
Because this method will reload your table view for every character which you type in your search field as the name itself suggests shouldReloadTableForSearchString
Edit:
Well you need to implement 2 delegate methods of UISearchBar
because all your UISearchDisplayController
delegate methods are same and those 2 methods are
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText;
This tells the delegate that the user changed the search text.
- (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
This is used if the text in a specified range should be replaced with given text.
and please note these methods are called several times, i mean for the every character added and deleted in the search bar and because you are setting the searchFetchedResultsController to nil every time the search text changes
just comment out this part, it will work well
/*if (searchFetchedResultsController_ != nil)
{
NSLog(@"Returned !nil searchController");
return searchFetchedResultsController_;
}*/
Upvotes: 3