首页 > 解决方案 > 取消按钮单击/ tableview reloadData 功能后,搜索栏从 tableview 标题中消失

问题描述

我在使用 Objective-C 的应用程序中使用 searchController 更改了 searchDisplayController 以支持 ios 8。

它在第一次尝试时效果很好,但是每当我单击搜索栏上的取消按钮时,动画完成后,我的搜索栏就会消失。我不希望它消失,每当我返回主屏幕并返回搜索栏屏幕时,它就会完美显示。

我已经尝试将搜索栏放在情节提要的表格视图之外,但不起作用。我还尝试了几乎所有我能找到的网络解决方案。

编辑:我在另一个 .m 文件中尝试了相同的代码块,它在该屏幕中运行良好,在我假设的取消按钮后,某些东西会覆盖-重新加载 tableview,但我找不到它是什么。会是什么?我错过了什么?

EDIT.2:tableview reloadData 功能使其消失,仍然没有解决方案。

这是关于搜索栏的代码

- (void)viewDidLoad {
    [super viewDidLoad];

    self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
    self.searchController.searchResultsUpdater = self;
    self.searchController.dimsBackgroundDuringPresentation = NO;
    self.searchController.searchBar.delegate = self;
    self.tableView.tableHeaderView =  self.searchController.searchBar;
    self.definesPresentationContext = YES;
    [self.searchController.searchBar sizeToFit];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];

    [self.searchController.searchBar setTranslucent:NO];
    [self.searchController.searchBar setBackgroundColor:_themeColor];
}


- (void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
    NSString *searchString = searchController.searchBar.text;
    [self searchForText:searchString];
    [self.tableView reloadData];
}
- (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope
{
  [self updateSearchResultsForSearchController:self.searchController];
}

- (void)searchForText:(NSString *)searchString
{
    [self filterContentForSearchText:searchString
    scope:[[self.searchController.searchBar scopeButtonTitles]
           objectAtIndex:[self.searchController.searchBar
                          selectedScopeButtonIndex]]];
}

标签: iosobjective-cuitableviewuisearchbaruisearchbardelegate

解决方案


推荐阅读