0

我按照这里的教程创建了一个搜索栏: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
4

3 回答 3

1

然后,假设如果您基于 搜索cellName,那么您NSPredicate应该是这样的:

[searchResults removeAllObjects];

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

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

试试这个,你的问题就解决了……

于 2012-12-31T11:24:19.777 回答
1

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

于 2012-12-31T18:04:22.173 回答
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
于 2013-01-01T01:38:07.557 回答