【发布时间】: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