【问题标题】:findObjectsInBackgroundWithBlock shows no resultsfindObjectsInBackgroundWithBlock 没有显示任何结果
【发布时间】:2013-09-01 12:24:13
【问题描述】:

我正在尝试使用 uisearchbar 在 parse.com 上搜索我的对象并执行“findObjectsInBackgroundWithBlock”。我在输出中得到了正确的结果,但它们没有出现在我的表格中。

我之前是在没有块的情况下执行此操作的,我的代码可以运行,它得到了正确的结果,但运行速度非常慢,并且我收到了一个警告,“警告:正在主线程上执行一个长时间运行的 Parse 操作”

我之前一直在使用代码:

- (void)filterResults:(NSString *)searchTerm {

[self.searchResults removeAllObjects];

PFQuery *query = [PFQuery queryWithClassName: @"Items"];
[query whereKeyExists:@"itemName"];  
[query whereKeyExists:@"itemDescription"]; 
[query whereKey:@"tags" containsString:searchTerm];

NSArray *results  = [query findObjects];
NSLog(@"%@", results);
NSLog(@"%u", results.count);

[self.searchResults addObjectsFromArray:results];

}

所以现在我尝试使用 findObjectsInBackgroundWithBlock 代替,我之前没有使用过块,所以这是我需要帮助的地方,这是我的新代码:

- (void)filterResults:(NSString *)searchTerm {

[self.searchResults removeAllObjects];

PFQuery *query = [PFQuery queryWithClassName: @"Items"];

[query whereKey:@"tags" containsString:searchTerm];

[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {

    NSLog(@"%@", objects);
    NSLog(@"%u", objects.count);
[self.searchResults addObjectsFromArray:objects];}];

这是我的更多代码

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

                      if (tableView == self.tableView) {


                          return self.objects.count;

                      } else {

                          return self.searchResults.count;

                      }


                  }

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {

                      NSString *uniqueIdentifier = @"cell";
                      HomeCell *cell = nil;

                      cell = (HomeCell *) [self.tableView dequeueReusableCellWithIdentifier:uniqueIdentifier];

                      if (!cell) {
                          NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"HomeCell" owner:nil options:nil];

                          for (id currentObject in topLevelObjects)
                          {
                              if([currentObject isKindOfClass:[HomeCell class]])
                              {
                                  cell = (HomeCell *)currentObject;
                                  break;
                              }
                          }
                      }
if (tableView != self.searchDisplayController.searchResultsTableView) {
    NSString *itemName = [object objectForKey:@"itemName"];
    NSString *itemDescription = [object objectForKey:@"itemDescription"];
    //cell.textLabel.text = last;


    cell.cellTitleLabel.text = itemName;
    cell.descriptionLabel.text = itemDescription;

    cell.priceLabel.text = [object objectForKey:@"price"];

    PFFile *thumbnail = [object objectForKey:@"imageFile"];
    PFImageView *thumbnailImageView = cell.imageFile;
    thumbnailImageView.image = [UIImage imageNamed:@"Facebook @2x.png"];
    thumbnailImageView.file = thumbnail;
    [thumbnailImageView loadInBackground];







}
if ([tableView isEqual:self.searchDisplayController.searchResultsTableView]) {

    PFObject *obj2 = [self.searchResults objectAtIndex:indexPath.row];
    PFQuery *query = [PFQuery queryWithClassName:@"Items"];
    PFObject *searchedItems = [query getObjectWithId:obj2.objectId];
    NSString *itemName = [searchedItems objectForKey:@"itemName"];
    NSString *itemDescription = [searchedItems objectForKey:@"itemDescription"];


    cell.cellTitleLabel.text = itemName;
    cell.descriptionLabel.text = itemDescription;

    cell.priceLabel.text = [searchedItems objectForKey:@"itemName"];
    PFFile *thumbnail = [searchedItems objectForKey:@"imageFile"];
    PFImageView *thumbnailImageView = cell.imageFile;
    thumbnailImageView.image = [UIImage imageNamed:@"Facebook @2x.png"];
    thumbnailImageView.file = thumbnail;
    [thumbnailImageView loadInBackground];
}




return cell;

任何帮助将不胜感激, 干杯

【问题讨论】:

  • 您是否在找到搜索结果后刷新了表格?
  • @enovav 是的,我试过 [self.tableView reloadData];但它仍然不起作用......还有其他想法吗?
  • tableView:numberOfRowsInSection:tableView:cellForRowAtIndexPath: 使用什么代码?
  • @enovav 更新了我的问题以显示更多代码
  • 您似乎没有在 cellForRowAtIndexPath 中设置单元格的任何属性。我不确定 HomeCell 有哪些属性,但您通常会设置标签属性的文本,例如 cell.textLabel.text = [object stringForKey:@"#some key#"]

标签: ios parse-platform pfquery


【解决方案1】:

为了更新表格视图,您需要在添加新的搜索结果后调用reloadData: 方法。确保在提供给findObjectsInBackgroundWithBlock: 的块中调用此方法,因为此代码块将在单独的线程上运行。这会导致该方法立即返回,并且此方法之后的代码将在块实际执行之前运行。 filterResults: 中的查找对象代码应如下所示:

[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    // This block is called from a background thread once the query has been executed
    NSLog(@"%@", objects);
    NSLog(@"%u", objects.count);
    [self.searchResults addObjectsFromArray:objects];
    // Refresh the table view on the main thread
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.tableView reloadData];
        [self.searchDisplayController.searchResultsTableView reloadData];
    });
}];

【讨论】:

  • 我正在尝试这种方法 enovav 但我仍然无法在 tableview 中得到结果。
  • 好的,您是否尝试在cellForRowAtIndexPath: 中的NSString *itemName = [object objectForKey:@"itemName"]; 行下方的行上添加断点并检查 itemName 变量的值?
  • 我刚刚再次更新了我的问题的开头,我之前一直使用 [query findObjects]; 接收正确的对象;但工作非常缓慢,我在 Xcode 中遇到错误,所以 [object objectForKey:@"itemName"];工作正常,
  • 如果您确定问题不在cellForRowAtIndexPath: 中,那么它可能在filterResults: 中。我在您发布的代码中看不到您在哪里调用了reloadData:,所以请您发布完整的filterResults: 方法吗?
  • 在您建议我这样做之前,我没有使用 reloadData。我使用了您在回答中建议的方法。我以前从未使用过块,我不太确定在执行块后如何重新加载数据,所以我想我只是在考虑其他人的建议。
【解决方案2】:

在创建单元格时区别于您的搜索表格视图和常规表格视图。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {
    if (tableView = self.tableView) {
        //Configure your cell normally
    }
    else {
        //Configure your cells using
        cell.someAttribute = self.searchResults[indexPath.row].someAttribute;
    }

【讨论】:

  • 它不起作用我添加了 [self.tableView reloadData];但它仍然不显示结果
  • 检查 searchResults 是否真的从 pfquery 获取对象。
  • 我刚刚更新了我的问题以显示更多代码。我很确定我正在以正确的方式配置单元格。
猜你喜欢
  • 2015-05-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-16
  • 1970-01-01
  • 2012-12-19
  • 1970-01-01
相关资源
最近更新 更多