William Rose
William Rose

Reputation: 47

How to add a search bar to a TableView that uses an array with NSDictionaries

What I've done so far to make a search bar work...

- (void)viewDidLoad
{

[self makeData];

[super viewDidLoad];

self.mySearchBar.delegate = self;
self.myTableView.delegate = self;
self.myTableView.dataSource = self;

//Feedback button code here
UIBarButtonItem *feedback;
feedback = [[UIBarButtonItem alloc] initWithTitle:@"Contact"   style:UIBarButtonItemStylePlain
target:self action:@selector(showEmail:)];
self.navigationItem.leftBarButtonItem = feedback;


[self.tableView reloadData];
self.tableView.scrollEnabled = YES;

}

-(void) makeData; {

list = [[NSMutableArray alloc] init];

[list addObject:[[NSMutableDictionary alloc]
                initWithObjectsAndKeys:@"Acceleration", @"name" ,
                @"acceleration.png", @"image" ,
                @"Acceleration", @"description" ,nil]];

[list addObject:[[NSMutableDictionary alloc]
                initWithObjectsAndKeys:@"Alternating Current", @"name" ,
                @"alternating current.png", @"image" ,
                @"Alternating Current", @"description" ,nil]];

[list addObject:[[NSMutableDictionary alloc]
                initWithObjectsAndKeys:@"Ampere", @"name" ,
                @"ampere.png", @"image" ,
                @"Ampere", @"description" ,nil]];

[list addObject:[[NSMutableDictionary alloc]
                initWithObjectsAndKeys:@"Amplitude", @"name" ,
                @"amplitude.png", @"image" ,
                @"Amplitude", @"description" ,nil]];


}

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
if (searchText.length == 0) {
    isFiltered = FALSE;
}
else
{
    isFiltered = TRUE;
    filteredList = [[NSMutableArray alloc] init];

    for (NSString *str in list) {
        NSRange stringRange = [str rangeOfString:searchText options:NSCaseInsensitiveSearch];

        if (stringRange.location != NSNotFound) {
            [filteredList addObject:str];
        }
    }
}
[self.myTableView reloadData];
}

-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
[self.myTableView reloadData];
}

Yet each time I try to type something in the app crashes and I get this message:

2013-05-29 11:36:09.734 PhysEX[4770:c07] -[__NSDictionaryM rangeOfString:options:]: unrecognized selector sent to instance 0x9468cd0
2013-05-29 11:36:09.735 PhysEX[4770:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSDictionaryM rangeOfString:options:]: unrecognized selector sent to instance 0x9468cd0'

How do I get my search to work?

Upvotes: 1

Views: 261

Answers (1)

Valent Richie
Valent Richie

Reputation: 5226

The following piece of code in textDidChange delegate is not correct:

for (NSString *str in list) {
    ...
}

It is because objects in the list array are not NSString but NSDictionary. Hence the error message saying that NSDictionary does not recognize the selector rangeOfString:options: because that selector is for NSString instance.

You need to search through NSDictionary values instead.

for (NSDictionary *theDictionary in list) {

    for (NSString *key in theDictionary) {
        NSString *value = [theDictionary objectForKey:key];
        NSRange stringRange = [value rangeOfString:searchText options:NSCaseInsensitiveSearch];

        if (stringRange.location != NSNotFound) {
            [filteredList addObject:theDictionary];
            break;
        }
   }
}

Upvotes: 1

Related Questions