简体   繁体   English

parse.com上的搜索功能将搜索结果覆盖在原始tableview上

[英]search function on parse.com overlays search results on original tableview

I'm pretty new at this and having a go using Parse.com as the backend server. 我在这方面很新,可以使用Parse.com作为后端服务器。 I'm building a database of vegetables, and want to perform a search on the list pulled from parse.com. 我正在建立一个蔬菜数据库,并希望对从parse.com提取的列表进行搜索。 I have it all working, except for one annoying thing... 除了一件烦人的事,我都工作了。

Now, I'm using storyboards and have created a custom cell which includes a PFImage thumbnail view, a label showing the vegetable, and then another label showing the season for the vegetable. 现在,我正在使用情节提要,并创建了一个自定义单元,其中包括PFImage缩略图视图,显示蔬菜的标签,然后是显示蔬菜季节的另一个标签。

When the viewcontroller is called, the list populates perfectly and lists the vegetables in alphabetical order. 调用viewcontroller时,列表将完美填充,并按字母顺序列出蔬菜。 Then I drag the window down to reveal the search bar. 然后,我向下拖动窗口以显示搜索栏。 I begin typing in a vegetable name, and as I do so the original table data rows begin disappearing (as they should), but the problem is the original table data sticks around. 我开始输入一个植物名,并且这样做,原始表数据行开始消失(应该如此),但是问题是原始表数据仍然存在。 So, for instance, I'll type "carrot", and all the rows disappear except the top row which still holds a thumbnail of an artichoke (and the label "Artichoke" as well). 因此,例如,我将键入“ carrot”,除第一行仍保留朝鲜蓟的缩略图(以及标签“ Artichoke”)之外的所有行均消失。 But overlayed on that row is also the word "Carrots", which is another vegetable in the list. 但在该行上还覆盖了“胡萝卜”一词,这是列表中的另一种蔬菜。 If I tap on it, it properly seques to my detail view controller showing carrots. 如果我轻按它,它将正确地固定到显示胡萝卜的详细信息视图控制器中。 So everything is working properly, but I can't figure out how to make it so the search results aren't being written over the top of the original data. 因此,一切工作正常,但我无法弄清楚如何制作,因此搜索结果不会写在原始数据的顶部。

Here's the code portions: 这是代码部分:

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

}

Lower in the code is my prepareForSeque code and other search methods. 代码下面是我的prepareForSeque代码和其他搜索方法。 I know it's a little ugly with repeated code, but I've been trying all sorts of things to fix my issue and wasn't going to get around to cleaning things up until I figured out the issue. 我知道重复的代码有点丑陋,但是我一直在尝试各种方法来解决我的问题,直到我弄清楚这个问题,才打算解决问题。 Also, I created a new column on parse.com's data browser called lowerCaseVegetable since the search is case sensitive. 另外,由于搜索区分大小写,因此我在parse.com的数据浏览器上创建了一个新的列,名为LowerCaseVegetable。 So the search is actually performed on that column, but is displayed using the normal "vegetable" column, which has the vegetable name capitalized. 因此,搜索实际上是在该列上执行的,但是使用正常的“蔬菜”列显示,该列的植物名称大写。

- (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]);
    }
}

I have a feeling I'm just making a stupid newbie mistake here, but I've only been at this since May, and specifically fighting this issue the last two weeks. 我有一种感觉,我只是在犯一个愚蠢的新手错误,但自5月以来我就一直在这样做,尤其是在最近两周内与这个问题作斗争。 I figured it was about time to ask for help. 我认为现在是时候寻求帮助了。

I just implemented search in my parse app and I think I see the problem: 我刚刚在我的解析应用中实现了搜索,我认为我看到了这个问题:

In your filterResults method try using findObjectsInBackgroundWithBlock instead of findObjects like this: 在您的filterResults方法中,尝试使用findObjectsInBackgroundWithBlock代替findObjects如下所示:

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

         [self.searchResults addObjectsFromArray:array];
        [self.searchDisplayController.searchResultsTableView reloadData];

    }];

prepareForSegue: 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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM