ios - 如何搜索 PFObject UITableView

标签 ios uitableview parse-platform

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

}

最佳答案

您的代码首先在 filterResults 中检索包含“by”字段和“title”字段以及“title”包含 searchTerm 的所有对象。

然后,当您创建一个单元格时,您会执行另一个查询(您永远不应该这样做,因为它现在对显示的每个单元格都有一个持久的操作。滚动此 View 将永远不起作用。此外; 您正在将标题与字符串 @"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 中进行查找。先准备您的数据,然后以最佳性能呈现它们。

关于ios - 如何搜索 PFObject UITableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18423934/

相关文章:

iOS 自动布局 : Dynamic height for table view cell

iphone - heightForRowAtIndexPath崩溃

PHP cURL 在本地工作,在 AWS 服务器上出现错误 77

ios - 注销之前不会保存当前用户的设置

objective-c - iAd通知*const__strong'到参数

ios - 为什么我在 iOS 11.2 上看不到 "Add to Homescreen"选项

ios - 了解 iOS 中的多线程

javascript - React-native SectionList 项目索引在部分之间继续

iphone - UIActivityController 是如何呈现的?

swift - 解析 PFSubclassing 不适用于 Swift