2013-09-01 150 views
0

我想使用uisearchbar搜索parse.com上的對象並執行'findObjectsInBackgroundWithBlock'。我在輸出中得到了正確的結果,但他們沒有出現在我的表格中。findObjectsInBackgroundWithBlock顯示沒有結果

我以前做這個不塊,我的代碼工作,它得到正確的結果,但走得很慢,我得到一個警告,「警告:正在主線程上執行的長時間運行的分析操作」

我使用的代碼以前是:

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

任何幫助將不勝感激, 歡呼聲從搜索表視圖,你經常的tableview

+0

你有沒有更新的表,你已經找到了搜索結果後? – shearnonsense

+0

@enovav是的我試過[self.tableView reloadData];但它仍然不起作用...任何其他想法? – Shayno

+0

你使用什麼代碼爲'tableView:numberOfRowsInSection:'和'tableView:cellForRowAtIndexPath:'? – shearnonsense

回答

5

爲了更新表視圖,您需要調用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]; 
    }); 
}]; 
+0

我想這個方法enovav,但我仍然無法在tableview中得到結果。 – Shayno

+0

好的,您是否嘗試在'NSString * itemName = [object objectForKey:@「itemName」];'cellForRowAtIndexPath:'中的行下面添加一個斷點並檢查itemName變量的值? – shearnonsense

+0

我剛剛更新了我的問題的開頭,我以前一直在使用[query findObjects]接收正確的對象;但工作非常緩慢,我在Xcode中出現錯誤,所以[object objectForKey:@「itemName」];工作正常, – Shayno

0

微分當您創建的細胞。

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

它不起作用我添加了[self.tableView reloadData];但它仍然不顯示結果 – Shayno

+0

檢查searchResults是否實際上從pfquery獲取對象。 – Kevin

+0

yes searchResults is getting objects – Shayno