You can first select the correct array in the dictionary by doing:
NSString *firstLetter = [searchString substringWithRange:(NSRange){0,1}];
NSArray *people = PersonsListSection[firstLetter];
Then you can filter down on the people by using NSPredicates
:
NSPredicate *namesBeginningWithKeyword = [NSPredicate predicateWithFormat:@"(name BEGINSWITH[cd] $letter)"];
NSArray *filteredPeople = [people filteredArrayUsingPredicate:[namesBeginningWithKeyword predicateWithSubstitutionVariables:@{@"letter": searchString}]]);
The question of how you make that be reflected in the tableview's content is a whole another question though.
- Typically, you'll want your view controller to be the UISearchBar's delegate and react to change using the
– (void)searchBar:textDidChange:
delegate method. - There you could just call your tableview's
- reloadData
method so it tries recompute its content, calling all of its dataSource method like- numberOfSectionsInTableView:
and so forth. - In these methods, in turn, you'll want to check whether some text was entered in the search bar and use the above tips to return the right sections/cells.