在PFQueryTableView中搜索不起作用 [英] Search in PFQueryTableView not working

查看:96
本文介绍了在PFQueryTableView中搜索不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有带有UISearchBar的PFQueryTableView,可以从我在Parse服务器上的用户类中搜索用户,但我从未使其正常运行.这是我的代码:

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

如果退出了所有用户的"featuredNote",此代码将显示所有用户-此部分工作正常,但在我搜索时不会显示搜索结果.我在搜索框中键入一个关键字,然后在键盘上按搜索键时,会触发 searchBarSearchButtonClicked 方法,仅此而已.我希望 queryForTable 会被调用,但不会.因此,该表仍将所有用户都显示为原始用户.我做错了什么,或者我在这里错过了什么?请帮忙.非常感谢.

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.

更新

由于我已将 [self.tableView reloadData]; 替换为 [self loadObjects]; ,因此我可以调用 queryForTable 之后, searchBarSearchButtonClicked .但是, tableView numberOfRowsInSection 仍然始终返回零.

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.

推荐答案

以下是代码

Here's the code you asked for from a project with a similar implementation need. I ended up using a UISearchBar with a separate UITableView to display the results. Make sure to make the proper Storyboard connections for the UISearchBar and UITableView when implementing. Also, the search bar's delegate should be set to the ViewController and the table view's delegate and dataSource should be set to the ViewController.

static NSString * const kSearchResultsTableCellReuseIdentifier = @"SearchResultsTableCellReuseIdentifier";

@interface ViewController ()
    @property (nonatomic, strong) NSArray *searchResults;
    @property (nonatomic, weak) IBOutlet UISearchBar *searchBar;
    @property (nonatomic, weak) IBOutlet UITableView *searchResultsTable;
@end

@implementation ViewController

...

#pragma mark - Protocol conformance

#pragma mark UISearchBarDelegate methods

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    if([searchText length] == 0)
    {
        [self dismissSearch];
    }
    else
    {
        [self handleSearchForString:searchText];
    }
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    // any logic related to Search button being clicked
}

#pragma mark UITableViewDataSource methods

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.searchResults count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [self.searchResultsTable dequeueReusableCellWithIdentifier:kSearchResultsTableCellReuseIdentifier];

    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kSearchResultsTableCellReuseIdentifier];
    }

    // Search result text to be displayed in table
    cell.textLabel.text = [self.searchResults objectAtIndex:indexPath.row].name;

    return cell;
}

#pragma mark UITableViewDelegate methods

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // any logic for when a cell in the search results table is selected

    [self dismissSearch];
}    

#pragma mark - Helpers

- (void)handleSearchForString:(NSString *)searchString
{
    // any logic for retrieving search results as an NSArray we'll call `results`

    self.searchResults = results;
    [self.searchResultsTable reloadData];

}

- (void)dismissSearch
{
    self.searchBar.text = @"";

    [self.searchBar performSelector: @selector(resignFirstResponder)
                      withObject: nil
                      afterDelay: 0.1];
}

@end

希望有帮助

这篇关于在PFQueryTableView中搜索不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆