I want to make a search bar. Is there anything wrong with this code?
- (void)viewDidLoad
{
[super viewDidLoad];
PFQuery *query = [PFUser query];
[query orderByAscending:@"username"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (error) {
NSLog(@"Error %@, %@", error, [error userInfo]);
}
else {
self.allUsers = objects;
[self.tableView reloadData];
}
}];
self.searchedUsers = [NSMutableArray arrayWithCapacity:[self.allUsers count]];
self.searchUsers = [[UISearchBar alloc]init];
self.doSearch = [[UISearchDisplayController alloc]initWithSearchBar:self.searchUsers contentsController:self];
self.doSearch.delegate = self;
self.doSearch.searchResultsDataSource = self;
self.doSearch.searchResultsDelegate = self;
}
- (void) fileterContentForSearchText:(NSString *)searchText {
[self.searchedUsers removeAllObjects];
for (NSDictionary *item in self.allUsers)
{
NSComparisonResult result = [[item objectForKey:@"name"] compare:searchText options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [searchText length])];
if (result == NSOrderedSame) {
[searchedUsers addObject:item];
}
}
}
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
[self filterContentForSearchText:searchString];
return YES;
}
Can someone please fix this code? I have followed tons of tutorials and none of them have worked. I would really appreciate it. Thank you!
You have a typo on the method name: fileterContentForSearchText:
Should be: filterContentForSearchText: