2014-04-16 32 views
0

我創建了包含超過300個城市的列表的表視圖,並且我想實現搜索欄。我前幾次實現了一個搜索欄,但是我從來沒有將搜索欄實現到由.plist文件填充的表視圖。在使用.plist文件填充的UITableView中使用UISearchBar

我會給你一些我有的代碼的例子。

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

表視圖顯示了所有我想要的信息,但是當我搜索的東西,它總是說「沒有結果」。

請問,有人可以幫助我嗎?

謝謝!

回答

1

嘗試使用NSPredicate的另一種方法。

而不是調用與SELF匹配,請嘗試調用一個或兩個您的密鑰。

例如...

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

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

這是否幫助?

+0

隨着@K應用程序崩潰......但是如果我用SELF替換@K並輸入確切的名稱,它會給出結果。 –

+0

對@ EnricEnrich延遲迴復抱歉,這仍然是一個問題嗎? – andrewbuilder

+0

不要擔心@andrewbuilder!昨天我用你的答案解決了它!謝謝! –