1

我试图弄清楚如何搜索/查询 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;

}
4

1 回答 1

2

What your code is doing is that first, in filterResults, you are retrieving all objects that contain the field "by" and the field "title" and where "title" contains searchTerm.

THEN, when you create a cell, you do another query (which you should never do, as it now has a long-lasting operation to do for each and every cell being displayed. Scrolling this view would never work. And besides; you are comparing the title with the string @"by", which I am pretty sure is not your intention.

So, your main problem is in they way you build your query, and your secondary problem is that you're doing this for every cell.

What you need to do is get all the data you want on the first search, and then iterate through those data in an array when displaying.

Something like:

- (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]);
        }
    }

Then, later in cellForRowAtIndexPath you use this array (no more queries) as the datasource for your data:

- (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;
    }

When working with databases, never do lookups in tableviewcells. Prepare your data first, and then present them with top performance.

于 2013-08-24T23:32:34.810 回答