搜索栏不返回搜索结果(NSpredicate)

时间:2012-12-31 10:19:52

标签: iphone objective-c uitableview uisearchbar

我在此处按照教程创建了一个搜索栏:http://www.appcoda.com/how-to-add-search-bar-uitableview/

但是我的搜索结果没有返回任何内容。我唯一能想到的是NSArray不可变,但是当改变它时,resultPredicate会给我一个不兼容的指针类型的错误。

.h

@interface plistTableViewController : UITableViewController<UISearchBarDelegate, UITableViewDataSource, UITableViewDelegate>

{
    NSArray *tabledata;
    NSArray *searchResults;
}

@end

的.m

#import "plistTableViewController.h"
#import "DetailViewController.h"
@interface plistTableViewController ()

@end

@implementation plistTableViewController


- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{

    NSString *mylist = [[NSBundle mainBundle] pathForResource:@"lodges" ofType:@"plist"];
    tabledata = [[NSArray alloc]initWithContentsOfFile:mylist];
    NSLog(@"%@", tabledata);


    [super viewDidLoad];

   }

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    if (tableView == self.searchDisplayController.searchResultsTableView) {
        return [searchResults count];

    } else {
        return [tabledata count];

    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *simpleTableIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if( cell == nil ) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Cell"];
    }

    if (tableView == self.searchDisplayController.searchResultsTableView) {
        cell.textLabel.text = [searchResults objectAtIndex:indexPath.row];
        cell.detailTextLabel.text = [searchResults objectAtIndex:indexPath.row];
    } else {
        cell.textLabel.text = [[tabledata objectAtIndex:indexPath.row]objectForKey:@"cellName"];
        cell.detailTextLabel.text = [[tabledata objectAtIndex:indexPath.row]objectForKey:@"cellSubtitle"];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }

    return cell;
}

// search bar code

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
    NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"SELF contains[cd] %@",searchText];

    searchResults = [tabledata filteredArrayUsingPredicate:resultPredicate];
}

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller
shouldReloadTableForSearchString:(NSString *)searchString
{
    [self filterContentForSearchText:searchString
                               scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
                                      objectAtIndex:[self.searchDisplayController.searchBar
                                                     selectedScopeButtonIndex]]];

    return YES;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        [self performSegueWithIdentifier: @"lodgedetail" sender: self];
    }
}

// end of searchbar code

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{

    NSIndexPath *indexPath = [[self tableView] indexPathForSelectedRow];
    NSDictionary *contactInfo = tabledata[indexPath.row];
    DetailViewController *dvc = (DetailViewController *)[segue destinationViewController];
    dvc.contactInfo = contactInfo;


    if ([segue.identifier isEqualToString:@"lodgedetail"]) {
        DetailViewController *dvc = segue.destinationViewController;

        NSIndexPath *indexPath = nil;

        if ([self.searchDisplayController isActive]) {
            indexPath = [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow];
            dvc.contactInfo = [searchResults objectAtIndex:indexPath.row];

        } else {
            indexPath = [self.tableView indexPathForSelectedRow];
            dvc.contactInfo = [tabledata objectAtIndex:indexPath.row];
        }
    }

}


@end

3 个答案:

答案 0 :(得分:1)

然后,假设您要根据cellName进行搜索,那么您的NSPredicate应该是这样的:

[searchResults removeAllObjects];

NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"cellName contains[cd] %@",searchText];

[searchResults addObjectsFromArray:[tabledata filteredArrayUsingPredicate:resultPredicate]];

试试这个,你的问题就会解决......

答案 1 :(得分:1)

它需要在%@周围使用单引号,否则您将基于对象而不是字符串进行比较。

答案 2 :(得分:0)

// searchbar code

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
    NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"cellName contains[cd] '%@'",searchText]];
    NSLog(@"%@", self.tabledata);

    self.searchResults = [NSMutableArray arrayWithArray:[self.tabledata filteredArrayUsingPredicate:resultPredicate]];
}

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller
shouldReloadTableForSearchString:(NSString *)searchString
{
    [self filterContentForSearchText:searchString
                               scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
                                      objectAtIndex:[self.searchDisplayController.searchBar
                                                     selectedScopeButtonIndex]]];

    return YES;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        [self performSegueWithIdentifier: @"lodgedetail" sender: self];
    }
}

// end of searchbar code

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{

    NSIndexPath *indexPath = [[self tableView] indexPathForSelectedRow];
    NSDictionary *contactInfo = tabledata[indexPath.row];
    DetailViewController *dvc = (DetailViewController *)[segue destinationViewController];
    dvc.contactInfo = contactInfo;


    if ([segue.identifier isEqualToString:@"lodgedetail"]) {
        DetailViewController *dvc = segue.destinationViewController;

        NSIndexPath *indexPath = nil;

        if ([self.searchDisplayController isActive]) {
            indexPath = [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow];
            dvc.contactInfo = [searchResults objectAtIndex:indexPath.row];

        } else {
            indexPath = [self.tableView indexPathForSelectedRow];
            dvc.contactInfo = [tabledata objectAtIndex:indexPath.row];
        }
    }

}


@end