I'm creating a table view with a list of more than 300 cities and I want to implement a search bar. I implemented a search bar a few times ago, but I never implemented a search bar to a table view populated by a .plist file.
I will give you some examples of the code that I have.
@property (nonatomic, strong) NSMutableArray *timezones;
@property (nonatomic, strong) NSMutableArray *filteredTimezones;
ViewDidLoad:
NSString *timezonesPath = [[NSBundle mainBundle] pathForResource:@"timezone" ofType:@"plist"];
self.timezones = [[NSMutableArray alloc]initWithContentsOfFile:timezonesPath];
self.filteredTimezones = [[NSMutableArray alloc] initWithCapacity:self.timezones.count];
searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
searchDisplayController.delegate = self;
searchDisplayController.searchResultsDataSource = self;
searchDisplayController.searchResultsDelegate = self;
self.tableView.tableHeaderView = searchBar;
NumberOfRowsInSection:
// Return the number of rows in the section.
if (tableView == searchDisplayController.searchResultsTableView) {
// Return the number of rows in the section.
return [self.filteredTimezones count];
} else {
// Return the number of rows in the section.
return [self.timezones count];
}
CellForRowAtIndex:
static NSString *CellIdentifier = @"TimezoneCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
}
NSString *timezone;
NSString *countrylist;
if (tableView == searchDisplayController.searchResultsTableView) {
timezone = [[self.filteredTimezones objectAtIndex:indexPath.row]objectForKey:@"city"];
countrylist = [[self.filteredTimezones objectAtIndex:indexPath.row]objectForKey:@"country"];
} else {
timezone = [[self.timezones objectAtIndex:indexPath.row]objectForKey:@"city"];
countrylist = [[self.timezones objectAtIndex:indexPath.row]objectForKey:@"country"];
}
// Configure the cell...
cell.textLabel.text = timezone;
cell.detailTextLabel.text = countrylist;
return cell;
Search Bar Methods:
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
[self.filteredTimezones removeAllObjects];
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"SELF contains[c] %@", searchText];
self.filteredTimezones = [NSMutableArray arrayWithArray: [self.timezones filteredArrayUsingPredicate:resultPredicate]];
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
[self filterContentForSearchText:searchString scope:[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
return YES;
}
.plist file example:
<array>
<dict>
<key>coordinatex</key>
<string>5.341111</string>
<key>coordinatey</key>
<string>-4.028056</string>
<key>number</key>
<string>11</string>
<key>timezone</key>
<string>Africa/Bamako</string>
<key>countryabbreviation</key>
<string></string>
<key>city</key>
<string>Abidjan</string>
<key>country</key>
<string>Ivory Coast</string>
</dict>
<dict>
<key>coordinatex</key>
<string>24.466665</string>
<key>coordinatey</key>
<string>54.416668</string>
<key>number</key>
<string>16</string>
<key>timezone</key>
<string>Asia/Dubai</string>
<key>countryabbreviation</key>
<string></string>
<key>city</key>
<string>Abu Dhabi</string>
<key>country</key>
<string>U.A.E.</string>
</dict>
</array>
The table view shows all the information that I want, but when I search something, it always says "No Results".
Please, can anybody help me?
Thanks!