我正在使用PFQueryTableView来搜索我的Parse数据库。一切似乎工作得很好,除了当我实际搜索结果没有意义。例如,我有三行,搜索值(truthIsName)是“a”,“ab”,“abc”,“g”。当我搜索“g”时,只显示“abc”。当我搜索“abc”时,“abc”出现。当我搜索“a”时,它似乎正常工作,“a”,“ab”和“abc”出现。如果需要它我可以提供更多示例,但我相信问题出在我的过滤器代码中,我无法弄清楚我做错了什么。
- (void)filterResults:(NSString *)searchTerm {
PFQuery *query = [PFQuery queryWithClassName: @"TruthIsData"];
[query whereKey:@"truthIsName" containsString:searchTerm];
//query.limit = 50;
[query findObjectsInBackgroundWithTarget:self
selector:@selector(callbackWithResult:error:)];
}
如果你需要,这里是完整的.m文件
#import "AllDataTableViewController.h"
#import <Parse/Parse.h>
@implementation AllDataTableViewController : PFQueryTableViewController
@synthesize searchBar;
@synthesize searchController;
@synthesize searchResults;
- (id)initWithCoder:(NSCoder *)aCoder
{
self = [super initWithCoder:aCoder];
if (self) {
// The className to query on
self.className = @"TruthIsData";
// The key of the PFObject to display in the label of the default cell style
self.textKey = @"truthIsName";
// Whether the built-in pull-to-refresh is enabled
self.pullToRefreshEnabled = YES;
// Whether the built-in pagination is enabled
self.paginationEnabled = NO;
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.tableView.tableHeaderView = self.searchBar;
self.searchController.searchResultsDataSource = self;
self.searchController.searchResultsDelegate = self;
self.searchController.delegate = self;
self.searchResults = [NSMutableArray array];
}
- (PFQuery *)queryForTable {
PFQuery *query = [PFQuery queryWithClassName:self.className];
if ([self.objects count] == 0) {
query.cachePolicy = kPFCachePolicyCacheThenNetwork;
}
return query;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [object objectForKey:@"truthIsName"];
cell.detailTextLabel.text = [NSString stringWithFormat:@"User: %@", [object objectForKey:@"username"]];
return cell;
}
- (void)callbackWithResult:(NSArray *)objects error:(NSError *)error
{
if(!error) {
[self.searchResults removeAllObjects];
[self.searchResults addObjectsFromArray:objects];
[self.searchDisplayController.searchResultsTableView reloadData];
}
}
- (void)filterResults:(NSString *)searchTerm {
PFQuery *query = [PFQuery queryWithClassName: @"TruthIsData"];
[query whereKey:@"truthIsName" containsString:searchTerm];
//query.limit = 50;
[query findObjectsInBackgroundWithTarget:self selector:@selector(callbackWithResult:error:)];
}
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
[self filterResults:searchString];
return YES;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (tableView == self.tableView) {
return self.objects.count;
} else {
return self.searchResults.count ;
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[super tableView:tableView didSelectRowAtIndexPath:indexPath];
}
@end
谢谢!
添加我得到的行
TutorialBase[764:20074] Queried Objects: (
"<TruthIsData:6ikJq0hhtc:(null)> {\n truthIsName = g;\n}"
) ((null))
答案 0 :(得分:0)
我不完全确定你班级的流程,但在我看来你根本没有使用查询的结果(存储在self.searchResults
中)。
更准确地说,您并未处理tableView:cellForRowAtIndexPath:object:
中的搜索结果。我希望这样的事情:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {
static NSString *CellIdentifier = @"Cell";
if (tableView == self.tableView) {
<actual implementation here>
} else {
<similar implementation but where you get the data from self.searchResults>
}
}
(与你在tableView:numberOfRowsInSection:
中所做的一致)
实际上,我要做的是覆盖这样的cellForRowAtIndexPath:
:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (tableView == self.tableView) {
//-- this will call into PFQueryTable and then into tableView:numberOfRowsInSection:object:
[super tableView:tableView cellForRowAtIndexPath:indexPath];
} else {
<similar implementation but where you get the data from self.searchResults>
}
}
这样你就不会进入用于searchResults的PFQueryTableView(它们完全显示在不同的表视图中)。
我不确定让同一个类同时充当PFQueryTableView
和UISearchDisplayController
的委托/数据源是个好主意 - 如果你有一个代码肯定会更具可读性后者数据源的专门课程。但这只是一个建议。
编辑:
这就是它的样子:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
if (tableView == self.tableView) {
//-- this will call into PFQueryTable and then into tableView:numberOfRowsInSection:object:
[super tableView:tableView cellForRowAtIndexPath:indexPath];
} else {
PFObject* object = self.searchResults[indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [object objectForKey:@"truthIsName"];
cell.detailTextLabel.text = [NSString stringWithFormat:@"User: %@", [object objectForKey:@"username"]];
return cell;
}