我试图弄清楚如何搜索/查询PFObject而不是用户。这是我迄今为止所做的和它的发现,但没有显示。我找到this post并跟着它,但没有达到运行结果,因为它没有显示结果。我无法弄清楚如何展示PFObject,以便我需要帮助:)
-(void)filterResults:(NSString *)searchTerm {
[self.searchResults removeAllObjects];
PFQuery *query = [PFQuery queryWithClassName:@"New"];
[query whereKeyExists:@"by"]; //this is based on whatever query you are trying to accomplish
[query whereKeyExists:@"title"]; //this is based on whatever query you are trying to accomplish
[query whereKey:@"title" containsString:searchTerm];
NSArray *results = [query findObjects];
NSLog(@"%@", results);
// NSLog(@"%u", results.count);
[self.searchResults addObjectsFromArray:results];
}
然后我试图在这里显示:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {
static NSString *CellIdentifier = @"Cell";
PFTableViewCell *cell = (PFTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[PFTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [object objectForKey:self.textKey];
if (tableView != self.searchDisplayController.searchResultsTableView) {
/* PFObject *title = [PFQuery queryWithClassName:@"title"];
PFQuery *query = [PFQuery queryWithClassName:@"New"];
PFObject *searchedUser = [query getObjectWithId:title.objectId]; NSString * usernameString = [searchedUser objectForKey:@"title"]; cell.textLabel.text = [NSString stringWithFormat:@"%@", usernameString];
*/
PFQuery *query = [PFQuery queryWithClassName:@"New"];
[query whereKey:@"title" equalTo:@"by"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
// The find succeeded.
NSLog(@"Successfully retrieved %d title", objects.count);
// Do something with the found objects
for (PFObject *object in objects) {
NSLog(@"%@", object.objectId);
}
} else {
// Log details of the failure
NSLog(@"Error: %@ %@", error, [error userInfo]);
}
}];
}
if ([tableView isEqual:self.searchDisplayController.searchResultsTableView]) {
PFQuery *query = [PFQuery queryWithClassName:@"New"];
[query whereKey:@"title" equalTo:@"by"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
// The find succeeded.
NSLog(@"Successfully retrieved %d title", objects.count);
// Do something with the found objects
for (PFObject *object in objects) {
NSLog(@"%@", object.objectId);
}
} else {
// Log details of the failure
NSLog(@"Error: %@ %@", error, [error userInfo]);
}
}];
/* //PFObject *obj2 = [self.searchResults objectAtIndex:indexPath.row];
PFQuery *query = [PFQuery queryWithClassName:@"New"];
PFObject *searchedUser = [query getObjectWithId:obj2.objectId];
NSString *first = [searchedUser objectForKey:@"title"];
NSString *last = [searchedUser objectForKey:@"by"];
cell.textLabel.text = [first substringToIndex:1];
NSString *subscript = last;
// cell.categoryName.text = [searchedUser objectForKey:@"category"];
*/
}
return cell;
}
答案 0 :(得分:2)
您的代码所做的是,首先,在filterResults中,您将检索包含字段“by”和字段“title”以及“title”包含searchTerm的所有对象。
然后,当你创建一个单元格时,你会做另一个查询(你永远不应该这样做,因为它现在有一个持久的操作来处理所显示的每个单元格。滚动这个视图永远不会工作。除此之外;您正在将标题与字符串@“by”进行比较,我非常确定这不是您的意图。
因此,您的主要问题在于您构建查询的方式,而您的第二个问题是您正在为每个单元格执行此操作。
您需要做的是在第一次搜索时获取所需的所有数据,然后在显示时迭代这些数据。
类似的东西:
- (void)viewDidLoad {
PFQuery *query = [PFQuery queryWithClassName:@"TestClass"];
query.cachePolicy = kPFCachePolicyNetworkOnly;
query.limit = 50;
[query whereKey:@"city" equalTo:chosenCity];
[query whereKey:@"sex" equalTo:@"female"];
[query findObjectsInBackgroundWithTarget:self selector:@selector(callbackLoadObjectsFromParse:)];
- (void)callbackLoadObjectsFromParse:(NSArray *)result error:(NSError *)error {
if (!error) {
NSLog(@"Successfully fetched %d entries", result.count);
self.allTestObjects = result;
} else {
NSLog(@"Error: %@ %@", error, [error userInfo]);
}
}
然后,稍后在cellForRowAtIndexPath中使用此数组(不再查询)作为数据的数据源:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
PFObject * testObject = [self.allTestObjects objectAtIndex:indexPath.row],
cell.textLabel.text = [testObject objectForKey:@"name"];
return cell;
}
使用数据库时,永远不要在tableviewcells中进行查找。首先准备好您的数据,然后以最佳性能呈现它们。