我已經基於我的代碼this示例。初始數據顯示在表格視圖中,但我不確定如何執行其餘部分以顯示過濾的數據。我沒有錯誤,搜索欄有效,但沒有過濾數據。這裏是我的plist XML:客觀c過濾器表視圖與來自plist的數據
<dict>
<key>Things to do</key>
<array>
<dict>
<key>activity</key>
<string>Watch movies or TV shows</string>
<key>keywords</key>
<array>
<string>tv shows</string>
<string>movies</string>
</array>
</dict>
<dict>
<key>activity</key>
<string>Go Hiking</string>
<key>keywords</key>
<array>
<string>hiking</string>
<string>mountains</string>
</array>
</dict>
<dict>
<key>activity</key>
<string>Video Games</string>
<key>keywords</key>
<array>
<string>video games</string>
<string>playstation</string>
<string>xbox</string>
<string>nintendo</string>
</array>
</dict>
</array>
</dict>
這裏是我的CategoriesViewController.m:
@interface CategoriesViewController() <UISearchDisplayDelegate>
@property (nonatomic, strong) NSMutableArray *tableData;
@property (nonatomic, strong) NSMutableArray *searchResults;
@property (weak, nonatomic) IBOutlet UISearchBar *searchBar;
@end
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"categories" ofType:@"plist"];
NSDictionary *categoriesDictionary = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
self.tableData = categoriesDictionary[@"Things to do"];
//I think this next line is supposed to make it appear on top of the original table
self.searchResults = [NSMutableArray arrayWithCapacity:[self.tableData count]];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if (tableView == self.searchDisplayController.searchResultsTableView)
{
cell.textLabel.text = [self.searchResults objectAtIndex:indexPath.row];
} else {
cell.textLabel.text = self.tableData[indexPath.row][@"activity"];
}
return cell;
}
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
[self.searchResults removeAllObjects];
NSPredicate *resultPredicate = [NSPredicate
predicateWithFormat:@"SELF contains[cd] %@",
searchText];
self.searchResults = [NSMutableArray arrayWithArray:[self.tableData filteredArrayUsingPredicate:resultPredicate]];
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller
shouldReloadTableForSearchString:(NSString *)searchString
{
[self filterContentForSearchText:searchString
scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
objectAtIndex:[self.searchDisplayController.searchBar
selectedScopeButtonIndex]]];
return YES;
}
這就像例子中,除了我改變了我的地方數據的來源。我如何根據關鍵字進行搜索?在這個例子中,我只搜索我的初始表視圖中包含的內容嗎?感謝您的時間...
其中你實際上重新加載tableview – CoolMonster