【问题标题】:How to keep text in search bar when dismiss UISearchDisplayController关闭 UISearchDisplayController 时如何在搜索栏中保留文本
【发布时间】:2014-06-04 22:30:30
【问题描述】:

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

- (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?

【问题讨论】:

    标签: ios ios7 uisearchbar uisearchdisplaycontroller


    【解决方案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
    

    【讨论】:

      猜你喜欢
      • 2013-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-26
      • 1970-01-01
      • 1970-01-01
      • 2013-09-30
      相关资源
      最近更新 更多