gpt4 book ai didi

ios - 在 PFQueryTableView 中搜索不起作用

转载 作者:行者123 更新时间:2023-11-29 02:10:43 25 4
gpt4 key购买 nike

我有 PFQueryTableView 和 UISearchBar 来从 Parse 服务器上的用户类中搜索用户,但我从未让它工作。这是我的代码:

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 被调用,但事实并非如此。因此,该表仍将所有用户显示为原始用户。我做错了什么或者我在这里遗漏了什么?请帮忙。非常感谢。

更新

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

最佳答案

你正在设置查询但你没有告诉它进行搜索,你需要做这样的事情:

[query whereKeyExists:@"featuredNote"];//this sets the query

[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {

//Your code here


}];//this PERFORMS the query

关于ios - 在 PFQueryTableView 中搜索不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29313349/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com