I have PFQueryTableView with UISearchBar to search users from my user class on Parse server but I never get it working. Here is my code:
SearchViewController.h
#import <Parse/Parse.h>
@interface PAWUserSearch1ViewController : PFQueryTableViewController
@end
SearchViewController.m
#import "SearchViewController.h"
#import "PAWAppDelegate.h"
@interface SearchViewController () <UIGestureRecognizerDelegate, UISearchBarDelegate, UISearchDisplayDelegate>
@property (strong, nonatomic) IBOutlet UISearchBar *searchBar;
@property (nonatomic, strong) UISearchDisplayController *searchController;
@end
@implementation SearchViewController
- (id)initWithStyle:(UITableViewStyle)style {
self = [super initWithStyle:style];
if (self) {
self.parseClassName = [PFUser parseClassName];
self.pullToRefreshEnabled = NO;
self.paginationEnabled = YES;
self.objectsPerPage = self.objects.count;
}
return self;
}
- (PFQuery *)queryForTable {
NSLog(@"searchbar text --> %@", self.searchBar.text);
PFQuery *query = [PFUser query];
if ([self.searchBar.text length]!=0) {
PFQuery *userQuery = [PFUser query];
[userQuery whereKey:kPAWParseUsernameKey matchesRegex:self.searchBar.text modifiers:@"i"];
[query whereKey:kPAWParseUserKey matchesKey:@"objectId" inQuery:userQuery];
} else {
[query whereKeyExists:@"featuredNote"]; //show only users with location detailed
}
return query;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSLog(@"number of users --> %i", self.objects.count);
return self.objects.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *identifier = @"reuseIdentifier";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:identifier];
if (!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:identifier];
}
// display user name
NSString *userNameWithLocation = [NSString stringWithFormat:@"@%@ -%@", [self.objects[indexPath.row] objectForKey:kPAWParseUsernameKey], [self.objects[indexPath.row] objectForKey:@"featuredNote"]];
cell.textLabel.text = userNameWithLocation;
cell.textLabel.textColor = [UIColor darkGrayColor];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 70.0;
}
- (void)viewDidLoad {
[super viewDidLoad];
//set nav
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Done", nil) style:UIBarButtonItemStylePlain target:self action:@selector(doneButtonPressed:)];
self.pullToRefreshEnabled = NO;
// Hide the search bar until user scrolls up
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
self.tableView.tableHeaderView = self.searchBar;
self.searchBar.delegate = self;
self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar
contentsController:self];
self.searchController.searchResultsDataSource = self;
self.searchController.searchResultsDelegate = self;
self.searchController.delegate = self;
CGPoint offset = CGPointMake(0, self.searchBar.frame.size.height);
self.tableView.contentOffset = offset;
self.searchBar.placeholder = NSLocalizedString(@"Username", nil);
}
- (IBAction)doneButtonPressed:(id)sender {
[self.presentingViewController dismissModalViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
NSLog(@"search pressed!!!!!!");
[searchBar resignFirstResponder];
//[self.tableView reloadData];
[self loadObjects];
}
@end
This code displays all users if their "featuredNote" exits - this part works fine but when I search it does't show search results. I type a keyword in search box and when I hit search on keyboard, the searchBarSearchButtonClicked method is fired but that is it. I expect queryForTable to be called but it is not. As a result, the table still shows all users as original. What have I done wrong or am I missing something here? Please help out. Thank you so much.
Update
Since I have replaced [self.tableView reloadData]; with [self loadObjects]; and I am able to call queryForTable after searchBarSearchButtonClicked. However, tableView numberOfRowsInSection still always returns zero.