我的代码来自this示例。初始数据显示在表视图中,但我不确定如何实现其余部分以显示过滤后的数据。我没有错误,搜索栏工作,但没有过滤数据。这是我的plist XML:
<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;
}
这就像示例,除了我改变了我的数据来源。如何根据关键字进行搜索?在这个例子中,我只搜索初始表视图中包含的内容吗?谢谢你的时间......
答案 0 :(得分:1)
没有更多代码就很难说,但我认为你需要根据搜索字符串显示/隐藏你的两个表视图。
如果该字符串为空,则会隐藏您的searchResultsTableView
并显示您正在亲切地调用whatEverYouCalledYourOtherTableView
的其他表格视图。
如果您的字符串不为空,则执行搜索,显示searchResultsTableView
,隐藏whatEverYouCalledYourOtherTableView
并重新加载searchResultsTableView
的数据。
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
if ([searchString isEqualToString:@""])
{
self.searchDisplayController.searchResultsTableView.hidden = YES;
self.searchDisplayController.whatEverYouCalledYourOtherTableView.hidden = NO;
}
else
{
[self filterContentForSearchText:searchString
scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
self.searchDisplayController.searchResultsTableView.hidden = NO;
self.searchDisplayController.whatEverYouCalledYourOtherTableView.hidden = YES;
[self.searchDisplayController.searchResultsTableView reloadData];
}
return YES;
}
您的filterContentForSearchText
也是错误的。你有一个字典数组而不是字符串。首先抓住字符串,然后运行搜索。您最好立即创建字符串字典,让搜索工作的最简单方法就是这样。
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
[self.searchResults removeAllObjects];
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"SELF contains[cd] %@", searchText];
NSMutableArray *activityArray = [[NSMutableArray alloc] init];
for (int i = 0; i < [self.tableData count]; i++)
{
[activityArray addObject:self.tableData[i][@"activity"]];
}
self.searchResults = [NSMutableArray arrayWithArray:[activityArray filteredArrayUsingPredicate:resultPredicate]];
}