使用 UISearchController 但未重新加载 tableView
use of UISearchController but tableView is not reloaded
我正在尝试添加搜索栏来搜索我的 tableView。只有一个 TableViewController 添加了 searchBar。我检查了所有现有的指南和苹果示例代码并按照说明进行操作,但不知何故我的 tableView 在输入搜索字符串后没有重新加载。我用 NSLog 检查了我的代码,我确定搜索文本已正确上传,并且使用 NSPredicate.I 过滤了新数组,在 [=16= 的末尾添加了 [self.tableView reloadData] ] 方法,但我的 tableViewController 仍然没有更新。我是 iOS 编程的新手,不确定我做错了什么。如果您能快速浏览一下我的代码并向我提供反馈,我们将不胜感激。这是摘要:
@property (nonatomic,strong) UISearchController *searchController;
@property (nonatomic,strong) NSArray *myList; //original data list
@property (nonatomic,strong) NSArray *mysearchResult; //filtered list
- (void)viewDidLoad {
[super viewDidLoad];
self.searchController=[[UISearchController alloc]initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater=self;
self.searchController.dimsBackgroundDuringPresentation=NO;
self.searchController.searchBar.delegate=self;
self.tableView.tableHeaderView=self.searchController.searchBar;
self.definesPresentationContext=YES;
[self.searchController.searchBar sizeToFit];
}
-(void)updateSearchResultsForSearchController:(UISearchController *)searchController {
NSString *searchString=self.searchController.searchBar.text;
NSPredicate *mypredicate = [NSPredicate predicateWithFormat:@"SELF contains[c] %@", searchString];
self.mysearchResult=[self.myList filteredArrayUsingPredicate:mypredicate];
[self.tableView reloadData];
}
"CellForRowAtIndexPath" tableView代码的方法:(类似"numberOfRowsInSection"更新)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
MyCustomCell codes.....
if (tableView==self.tableView) {
cell.cellLabel.text=[self.myList objectAtIndex:indexPath.row];
} else
cell.cellLabel.text=[self.mysearchResult objectAtIndex:indexPath.row];
return cell;
}
第一次在填充 tableView 之前使用 mysearchResult
而不是 myList
。此时两个数组中的数据应该相同。
self.mysearchResult = [[NSArray alloc] initWithArray:myList];
现在使用 predicate 后你正在更新你的 mysearchResult
这很好。当您再次取消搜索时,从实际上包含所有原始数据的 myList
填充 mysearchResult
。
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
[self.mysearchResult count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
MyCustomCell codes.....
//Remove your If condition from here
cell.cellLabel.text=[self.mysearchResult objectAtIndex:indexPath.row];
return cell;
}
试试这个,可能会有用:
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
NSString *searchString=self.searchController.searchBar.text;
NSPredicate *mypredicate = [NSPredicate predicateWithFormat:@"SELF contains[c] %@", searchString];
self.mysearchResult=[self.myList filteredArrayUsingPredicate:mypredicate];
self.searchBar.scopeButtonTitles = self.mysearchResult;
}
我正在尝试添加搜索栏来搜索我的 tableView。只有一个 TableViewController 添加了 searchBar。我检查了所有现有的指南和苹果示例代码并按照说明进行操作,但不知何故我的 tableView 在输入搜索字符串后没有重新加载。我用 NSLog 检查了我的代码,我确定搜索文本已正确上传,并且使用 NSPredicate.I 过滤了新数组,在 [=16= 的末尾添加了 [self.tableView reloadData] ] 方法,但我的 tableViewController 仍然没有更新。我是 iOS 编程的新手,不确定我做错了什么。如果您能快速浏览一下我的代码并向我提供反馈,我们将不胜感激。这是摘要:
@property (nonatomic,strong) UISearchController *searchController;
@property (nonatomic,strong) NSArray *myList; //original data list
@property (nonatomic,strong) NSArray *mysearchResult; //filtered list
- (void)viewDidLoad {
[super viewDidLoad];
self.searchController=[[UISearchController alloc]initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater=self;
self.searchController.dimsBackgroundDuringPresentation=NO;
self.searchController.searchBar.delegate=self;
self.tableView.tableHeaderView=self.searchController.searchBar;
self.definesPresentationContext=YES;
[self.searchController.searchBar sizeToFit];
}
-(void)updateSearchResultsForSearchController:(UISearchController *)searchController {
NSString *searchString=self.searchController.searchBar.text;
NSPredicate *mypredicate = [NSPredicate predicateWithFormat:@"SELF contains[c] %@", searchString];
self.mysearchResult=[self.myList filteredArrayUsingPredicate:mypredicate];
[self.tableView reloadData];
}
"CellForRowAtIndexPath" tableView代码的方法:(类似"numberOfRowsInSection"更新)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
MyCustomCell codes.....
if (tableView==self.tableView) {
cell.cellLabel.text=[self.myList objectAtIndex:indexPath.row];
} else
cell.cellLabel.text=[self.mysearchResult objectAtIndex:indexPath.row];
return cell;
}
第一次在填充 tableView 之前使用 mysearchResult
而不是 myList
。此时两个数组中的数据应该相同。
self.mysearchResult = [[NSArray alloc] initWithArray:myList];
现在使用 predicate 后你正在更新你的 mysearchResult
这很好。当您再次取消搜索时,从实际上包含所有原始数据的 myList
填充 mysearchResult
。
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
[self.mysearchResult count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
MyCustomCell codes.....
//Remove your If condition from here
cell.cellLabel.text=[self.mysearchResult objectAtIndex:indexPath.row];
return cell;
}
试试这个,可能会有用:
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
NSString *searchString=self.searchController.searchBar.text;
NSPredicate *mypredicate = [NSPredicate predicateWithFormat:@"SELF contains[c] %@", searchString];
self.mysearchResult=[self.myList filteredArrayUsingPredicate:mypredicate];
self.searchBar.scopeButtonTitles = self.mysearchResult;
}