我想使用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
你有沒有更新的表,你已經找到了搜索結果後? – shearnonsense
@enovav是的我試過[self.tableView reloadData];但它仍然不起作用...任何其他想法? – Shayno
你使用什麼代碼爲'tableView:numberOfRowsInSection:'和'tableView:cellForRowAtIndexPath:'? – shearnonsense