我有一个UISearchController,它可以搜索表视图中显示的一组数据。我希望用户能够过滤出信息并能够滚动搜索,这意味着我希望他们键入的文本保留在uisearch栏中,直到他们按“取消”为止。
为此,我在updateSearchResultsForSearchController中只有一个全局变量,该变量已更新为searchBar.text,我在searchBarTextDidEndEditing中使用了该变量,将其设置回searchBar.text。
这里有一些代码:
- (void)viewDidLoad {
...
//init search controller
_searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
_searchController.searchResultsUpdater = self;
_searchController.searchBar.delegate = self;
[_searchController.searchBar sizeToFit];
//adds search bar to top header of table view
_tableViewJobList.tableHeaderView = self.searchController.searchBar;
[_tableViewJobList setContentOffset:CGPointMake(0, _searchController.searchBar.frame.size.height)];
}
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
//temp_search is variable updated with searchBar.text
NSString *searchString = searchController.searchBar.text;
temp_search = searchString;
//search stuff and reload data
...
}
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar {
searchBar.text = temp_search;
}
这在我按下背景时有效,但是当我输入然后按下背景时,uisearchbar会重置。当我也按下Enter键时,绝对会调用searchBarTextDidEndEditing。我觉得我缺少明显的东西!