gpt4 book ai didi

ios - 如何在关闭 UISearchDisplayController 时将文本保留在搜索栏中

转载 作者:可可西里 更新时间:2023-11-01 03:34:27 31 4
gpt4 key购买 nike

我在我的应用程序中使用 UISearchDisplayController。当用户在返回的搜索结果中选择一个项目时,我停用了 UISearchDisplayController。停用 Controller 会清除用户输入的文本。我想把它放在那里。我尝试在 Controller 停用后通过再次设置将文本分配回 UISearchBar。文本确实出现在搜索栏中,但这将导致 UISearchDisplayController 再次激活,即使我已禁用委托(delegate)!这个问题只发生在 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?

最佳答案

这是一些工作代码的示例:

#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

关于ios - 如何在关闭 UISearchDisplayController 时将文本保留在搜索栏中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23187573/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com