我正在尝试过滤UISearchController中的搜索栏,但它没有过滤?
@interface TableViewController () <UISearchBarDelegate>
@property (nonatomic, strong) UISearchController *searchController;
@property (nonatomic, strong) NSMutableArray *searchResults; // Filtered search results
@property (nonatomic, strong) NSMutableArray *objects;
@end
@implementation TableViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.searchController.searchBar.frame = CGRectMake(0, 0, 0, 44.0);
self.searchController.searchBar.delegate = self;
self.tableView.tableHeaderView = self.searchController.searchBar;
self.definesPresentationContext = YES;
self.searchController.searchBar.barTintColor = [UIColor yellowColor];
self.objects = [@[@"iOS",@"is",@"kool"]mutableCopy];
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
}
- (void)searchThroughResults
{
NSPredicate *resultsPredicate = [NSPredicate predicateWithFormat:@"SELF contains [search] %@", self.searchController.searchBar.text];
[self.searchResults filteredArrayUsingPredicate:resultsPredicate];
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
[self searchThroughResults];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
if (tableView == self.tableView) {
return self.objects.count;
} else {
[self searchThroughResults];
return self.searchResults.count;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
// Configure the cell...
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
if (tableView == self.tableView) {
cell.textLabel.text = self.objects[indexPath.row];
} else {
cell.textLabel.text = self.searchResults[indexPath.row];
}
return cell;
}
我做错了什么?为什么tableView没有被过滤?我也发布在:https://github.com/Kalson/iOS-Study/tree/master/SearchBar
答案 0 :(得分:0)
也许您需要在更改Array self.searchResults后重新加载UITableView? 尝试
- (void)searchThroughResults
{
NSPredicate *resultsPredicate = [NSPredicate predicateWithFormat:@"SELF contains [search] %@", self.searchController.searchBar.text];
[self.searchResults filteredArrayUsingPredicate:resultsPredicate];
[self.tableView reloadData];
}
答案 1 :(得分:0)
虽然您已添加了UISearchBarDelegate,但您尚未添加UISearchControllerDelegate或UISearchResultsUpdating委托。因此,请将这两个添加到角度括号中,并在.m文件顶部用逗号分隔。
答案 2 :(得分:0)
尝试在检查if (tableView == self.tableView)
的if语句中添加断点。如果它没有传递那个if语句,那么用这个if (![self.searchController.searchBar.text isEqualToString:@""]) {
替换他。我尝试过一些简单的项目并且有效。告诉我它是否对你有帮助,如果我不发布我的测试项目的更多代码。