在使用.plist文件填充的UITableView中使用UISearchBar

时间:2014-04-16 21:31:15

标签: ios objective-c uitableview uisearchbar

我创建了一个包含超过300个城市列表的表格视图,我想实现一个搜索栏。我几次实现了一个搜索栏,但我从来没有在一个.plist文件填充的表视图中实现搜索栏。

我将举例说明我的代码。

@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;

搜索栏方法:

- (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文件示例:

<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>

表格视图显示了我想要的所有信息,但是当我搜索某些内容时,它总是说&#34;没有结果&#34;。

拜托,有人可以帮助我吗?

谢谢!

1 个答案:

答案 0 :(得分:1)

尝试使用NSPredicate的其他方法。

不要针对SELF调用匹配,而是尝试针对您的一个或两个密钥进行调用。

例如......

NSString *keyCity = @"city";
NSString *keyCountry = @"country";

NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"(@K contains[c] %@) OR (@K contains[c] %@)", keyCity, searchText, keyCountry, searchText];

这有帮助吗?