我试图弄清楚如何搜索/查询 PFObject 而不是用户。到目前为止,这是我必须要做的,它的发现但没有显示。我找到了这篇文章并关注了它,但没有得到运行结果,因为它没有显示结果。我不知道如何显示 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;
}