我在这方面很陌生,并且正在使用Parse.com作为后端服务器。我正在构建一个蔬菜数据库,并希望对从parse.com提取的列表进行搜索。除了一件令人烦恼的事情外,我把它全部都搞定了......
现在,我正在使用故事板并创建了一个自定义单元格,其中包括PFImage缩略图视图,显示蔬菜的标签,以及显示蔬菜季节的另一个标签。
调用viewcontroller时,列表会完美填充并按字母顺序列出蔬菜。然后我向下拖动窗口以显示搜索栏。我开始输入一个蔬菜名称,并且当我这样做时,原始表格数据行开始消失(正如他们应该的那样),但问题是原始表格数据是否存在。因此,举例来说,我会键入"胡萝卜",除了顶行仍然保留朝鲜蓟的缩略图(以及标签"朝鲜蓟" as)之外,所有行都会消失。好)。但覆盖在那一行上的还有“#34; Carrots"”这个词,这是该列表中的另一种蔬菜。如果我点击它,它会正确地显示我的详细视图控制器显示胡萝卜。所以一切都运转正常,但我无法弄清楚如何制作它,因此搜索结果不会写在原始数据的顶部。
这是代码部分:
- (void)viewDidLoad
{
self.tableView.backgroundColor = [UIColor clearColor];
self.tableView.backgroundView=[[UIImageView alloc] initWithImage:
[UIImage imageNamed:@"bg.jpg"]];
[super viewDidLoad];
//add the search bar
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
self.tableView.tableHeaderView = self.searchBar;
self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self];
self.searchController.searchResultsDataSource = self;
self.searchController.searchResultsDelegate = self;
self.searchController.delegate = self;
CGPoint offset = CGPointMake(0, self.searchBar.frame.size.height);
self.tableView.contentOffset = offset;
self.searchResults = [NSMutableArray array];
//done adding search bar
}
- (PFQuery *)queryForTable
{
PFQuery *query = [PFQuery queryWithClassName:self.parseClassName];
// If no objects are loaded in memory, we look to the cache first to fill the table
// and then subsequently do a query against the network.
/* if ([self.objects count] == 0) {
query.cachePolicy = kPFCachePolicyCacheThenNetwork;
}*/
[query orderByAscending:@"vegetable"];
return query;
}
// Override to customize the look of a cell representing an object. The default is to display
// a UITableViewCellStyleDefault style cell with the label being the first key in the object.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object
{
static NSString *simpleTableIdentifier = @"VegetableCell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
// more search stuff
if (tableView == self.tableView) {
cell.backgroundColor = cell.backgroundColor;
}
else if(tableView == self.searchDisplayController.searchResultsTableView) {
PFObject *searchedVeggie = [self.searchResults objectAtIndex:indexPath.row];
cell.textLabel.text = [searchedVeggie objectForKey:@"vegetable"];
cell.backgroundColor = [UIColor whiteColor];
tableView.rowHeight = self.tableView.rowHeight;
}
PFFile *thumbnail = [object objectForKey:@"vegetableImageFile"];
PFImageView *thumbnailImageView = (PFImageView*)[cell viewWithTag:100];
thumbnailImageView.image = [UIImage imageNamed:@"placeholder.jpg"];
thumbnailImageView.file = thumbnail;
[thumbnailImageView loadInBackground];
UILabel *vegetableName = (UILabel*) [cell viewWithTag:101];
vegetableName.text = [object objectForKey:@"vegetable"];
UILabel *vegetableSeason = (UILabel*) [cell viewWithTag:102];
vegetableSeason.text = [object objectForKey:@"vegetableSeason"];
return cell;
}
代码中较低的是我的prepareForSeque代码和其他搜索方法。我知道重复的代码有点难看,但是我一直在尝试各种各样的事情来解决我的问题,并且在我弄清楚问题之前我还没有去清理。此外,我在parse.com的数据浏览器中创建了一个名为lowerCaseVegetable的新列,因为搜索区分大小写。因此,搜索实际上是在该列上执行的,但是使用普通"蔬菜"列,其蔬菜名称大写。
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"showVegetableDetail"]) {
if (self.searchDisplayController.active) {
NSLog(@"Search Display Controller");
VeggieDetailViewController *destViewController = segue.destinationViewController;
PFObject *object = [self.searchResults objectAtIndex: self.searchDisplayController.searchResultsTableView.indexPathForSelectedRow.row];
Vegetables *vegetables = [[Vegetables alloc] init];
vegetables.vegetable = [object objectForKey:@"vegetable"];
vegetables.vegetableInfo = [object objectForKey:@"vegetableInfo"];
vegetables.vegetableImageFile = [object objectForKey:@"vegetableImageFile"];
vegetables.vegetableSeason = [object objectForKey:@"vegetableSeason"];
destViewController.vegetables = vegetables;
} else {
NSLog(@"Default Display Controller");
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
VeggieDetailViewController *destViewController = segue.destinationViewController;
PFObject *object = [self.objects objectAtIndex:indexPath.row];
Vegetables *vegetables = [[Vegetables alloc] init];
vegetables.vegetable = [object objectForKey:@"vegetable"];
vegetables.vegetableInfo = [object objectForKey:@"vegetableInfo"];
vegetables.vegetableImageFile = [object objectForKey:@"vegetableImageFile"];
vegetables.vegetableSeason = [object objectForKey:@"vegetableSeason"];
destViewController.vegetables = vegetables;
}
}
}
// other search stuff
-(void)filterResults:(NSString *)searchTerm {
[self.searchResults removeAllObjects];
PFQuery *query = [PFQuery queryWithClassName: @"Vegetables"];
[query whereKeyExists:@"lowerCaseVegetable"];
[query whereKey:@"lowerCaseVegetable" containsString:searchTerm];
NSArray *results = [query findObjects];
[self.searchResults addObjectsFromArray:results];
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
[self filterResults:searchString];
return YES;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (tableView == self.tableView) {
return self.objects.count;
} else {
return self.searchResults.count;
}
}
-(void)callbackLoadObjectsFromParse:(NSArray *)result error:(NSError *)error {
if (!error) {
[self.searchResults removeAllObjects];
[self.searchResults addObjectsFromArray:result];
[self.searchDisplayController.searchResultsTableView reloadData];
} else {
// NSLog(@”Error: %@ %@”, [error userInfo]);
}
}
我有一种感觉,我只是在这里犯了一个愚蠢的新手错误,但是自从五月以来我一直都是这样做的,并且在过去的两周里专门针对这个问题。我觉得是时候寻求帮助了。
答案 0 :(得分:0)
我刚刚在我的解析应用中实现了搜索,我认为我发现了问题:
在filterResults
方法中尝试使用findObjectsInBackgroundWithBlock
代替findObjects
,如下所示:
[query findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error){
[self.searchResults addObjectsFromArray:array];
[self.searchDisplayController.searchResultsTableView reloadData];
}];
prepareForSegue:
Vegetables *vegetables = [[Vegetables alloc] init];
vegetables.vegetable = [self.selectedObject objectForKey:@"vegetable"];
vegetables.vegetableInfo = [self.selectedObject objectForKey:@"vegetableInfo"];
vegetables.vegetableImageFile = [self.selectedObject objectForKey:@"vegetableImageFile"];
vegetables.vegetableSeason = [self.selectedObject objectForKey:@"vegetableSeason"];
destViewController.vegetables = vegetables;