如何在退出UISearchDisplayController时保留搜索栏中的文本

7

我在我的应用程序中使用了UISearchDisplayController。当用户选择返回的搜索结果中的项目时,我会停用UISearchDisplayController。停用控制器会清除用户输入的文本。我想保留它。在控制器被停用后,我尝试通过再次设置它来将文本赋回到UISearchBar中。文本确实出现在搜索栏中,但这会导致UISearchDisplayController重新激活,即使我已经禁用了代理!这个问题只发生在iOS 7上。在iOS 7之前,下面的代码运行得很好。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSUInteger row = [indexPath row];
NSString *term = [keywordSuggestion objectAtIndex:row];

[search resignFirstResponder];
[self handleSearchForTerm:term];
}

-(void)handleSearchForTerm:(NSString *)searchTerm {

[searchDisplayController setActive:NO animated:YES]; //searchbar text will be lost

searchDisplayController.delegate = nil;
search.text = term;
searchDisplayController.delegate = self;
}

有没有一种方法可以设置UISearchBar的文本,而不必使与之关联的UISearchDisplayController变为活动状态?
1个回答

1
这是一段工作代码的示例:
#import "RBTableViewController.h"

@interface RBTableViewController () <UISearchDisplayDelegate>

@end

@implementation RBTableViewController {
    UISearchBar *_searchBar;
    UISearchDisplayController *_searchController;
    NSMutableArray *_searchResults;
    NSMutableArray *_model;
    NSString *_cachedSearchTerm;
}

- (NSMutableArray *)currentModel {
    return _searchController.isActive ? _searchResults : _model;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    _searchResults = [[NSMutableArray alloc] init];
    _model = [[NSMutableArray alloc] init];

    for (int i = 0; i < 10; i++) {
        [_model addObject:[NSString stringWithFormat:@"item %d", i]];
    }

     _searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
    _searchController = [[UISearchDisplayController alloc] initWithSearchBar:_searchBar contentsController:self];
    _searchController.searchResultsDataSource = self;
    _searchController.searchResultsDelegate = self;
    _searchController.delegate = self;
    [_searchController.searchResultsTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    self.tableView.tableHeaderView = _searchBar;
}

- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller {
    controller.searchBar.text = _cachedSearchTerm;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == _searchController.searchResultsTableView) {
        _cachedSearchTerm = _searchBar.text;
        [_searchController setActive:NO animated:YES];
        [self filterResults:_cachedSearchTerm];
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [[self currentModel] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.textLabel.text = [self currentModel][indexPath.row];
    return cell;
}

- (void)filterResults:(NSString *)searchTerm {
    [_searchResults removeAllObjects];
    [_searchResults addObjectsFromArray:[_model filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF contains[s] %@", searchTerm]]];
    [_searchController.searchResultsTableView reloadData];
}

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
    [self filterResults:searchString];
    return YES;
}

- (void)searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller {
    if (_cachedSearchTerm) {
        controller.searchBar.text = _cachedSearchTerm;
    }
}

@end

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接