【发布时间】:2018-06-28 08:15:01
【问题描述】:
我的 IOS 应用中有一个tableView 列表。我想用UISearchController 搜索我的tableView 列表。默认情况下,列表会正确显示。
当我激活搜索控制器(点击UISearchController)时,我现有的列表将消失,当我输入相关关键字时,结果将相应显示。
但是当我查看相关搜索结果时,searchController 将被停用并返回默认列表。
有什么想法吗?
- (void)viewDidLoad {
self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater = self;
self.searchController.delegate = self;
self.searchController.searchBar.delegate = self;
self.searchController.searchBar.barTintColor = ThemeLightGrayColor;
self.searchController.hidesNavigationBarDuringPresentation = NO;
[self.searchController.searchBar.layer setBorderColor:[UIColor colorWithRed:229.0/255 green:229.0/255 blue:229.0/255 alpha:1].CGColor];
[self.searchController.searchBar setPlaceholder:@"Search"];
[self.searchController.searchBar.layer setBorderWidth:0.5];
[self.searchController.searchBar setKeyboardType:UIKeyboardTypeDefault];
[[UITextField appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setDefaultTextAttributes:@{ NSFontAttributeName: [UIFont fontWithName:@"Bitter" size:14]}];
[self.searchController.searchBar sizeToFit];
_searchResultArr=[NSMutableArray array];
_tableView.tableHeaderView = ({
UIView *view = [[UIView alloc] init];
view.frame = CGRectMake(0, DCNaviH, 0, self.searchController.searchBar.frame.size.height + 50);
_segPromotion = [[UISegmentedControl alloc] initWithItems:@[@"All",@"Near Me",@"Coming Soon"]];
_segPromotion.selectedSegmentIndex = 0;
_segPromotion.backgroundColor = ThemeWhiteColor;
_segPromotion.frame = CGRectMake(10, self.searchController.searchBar.frame.origin.y+5 + self.searchController.searchBar.frame.size.height , ScreenW-20, 30);
[_segPromotion setTitleTextAttributes:@{NSFontAttributeName : [UIFont fontWithName:@"Bitter" size:13],NSForegroundColorAttributeName: ThemeDarkBlueColor } forState:UIControlStateNormal];
[_segPromotion setTitleTextAttributes:@{NSFontAttributeName : [UIFont fontWithName:@"Bitter" size:13],NSForegroundColorAttributeName : ThemeWhiteColor} forState:UIControlStateSelected];
[_segPromotion addTarget:self action:@selector(SegmentChangeViewValueChanged:) forControlEvents:UIControlEventValueChanged];
[view addSubview:self.searchController.searchBar];
[view addSubview:_segPromotion];
view;
});
[self.view addSubview:_tableView];
}
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
NSString *searchString = searchController.searchBar.text;
[self searchForText:searchString];
[self.tableView reloadData];
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
[self updateSearchResultsForSearchController:self.searchController];
}
- (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope
{
[self updateSearchResultsForSearchController:self.searchController];
}
- (void)searchForText:(NSString *)searchString
{
[self filterContentForSearchText:searchString
scope:[self.searchBar scopeButtonTitles][self.searchBar.selectedScopeButtonIndex]];
}
【问题讨论】:
-
您的问题不清楚...
searchForText是做什么的?当您选择搜索结果时会发生什么? -
嗨 AnthoPak,更新了 searchForText 功能。当我尝试点击搜索结果时,UISearchController 中的取消按钮将消失,列表将重置为默认列表。
-
哼...也许你可以尝试加
self.searchController.obscuresBackgroundDuringPresentation = NO; -
嗨 AnthoPak,非常感谢,它的作品!!!但是单击搜索结果后 UISearchController 栏将保留(预期结果:单击所选结果后将推送到另一个 ViewController 页面)。点击 didSelectRowAtIndexPath 后如何隐藏它?
-
我找到了这篇文章,它帮助我解决了上述问题。 stackoverflow.com/questions/35093042/…
标签: ios objective-c uisearchcontroller