iOS 中的 SearchController 11. 无法触摸任何 tableView 单元格
SearchController in iOS 11. Can't touch in any tableView cell
搜索控制器的系统行为:
- 转到 iPhone 设置
- 滑动以显示搜索栏
- 点击搜索栏
- TableView 现在是灰色的,如果我触摸任何地方(不在 searchBar 中),searchBar 将隐藏
- 我输入任何文本,tableView 现在颜色正常,我可以点击任何搜索到的单元格。
在我的应用程序中,点击任何文本后 tableView 仍然是灰色的。我无法点击任何搜索到的单元格。
不同之处还在于,当我搜索时,我会使用 updateSearchResultsForSearchController
更新旧的 tableView(搜索后我没有不同的 tableView),然后再使用 reloadData。
问题是如何在搜索控制器中输入任何文本后隐藏此灰色视图并提供点击单元格的机会?
我创建搜索控制器的方式:
UISearchController * search = [[UISearchController alloc] initWithSearchResultsController:nil];
search.searchResultsUpdater = self;
self.navigationItem.searchController = search;
self.navigationItem.hidesSearchBarWhenScrolling = YES;
self.navigationItem.searchController.searchBar.barTintColor = [UIColor blueColor];
self.navigationItem.searchController.searchBar.delegate = self;
以及我如何更新 tableView 单元格:
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
self.actualSearchController = searchController;
self.filteredData = [self filterContentForSearchText:searchController.searchBar.text];
[self.tableView reloadData];
}
- (NSMutableArray<MySectionContainter *> *)filterContentForSearchText:(NSString *)text {
NSMutableArray<MySectionContainter *> *sections = [NSMutableArray array];
for (MySectionContainter *section in self.tableData) {
NSArray *objects = [sectionInfo.rowObjects filteredArrayUsingPredicate:[self.filterSource predicateForSearching:text]];
if ([objects count] > 0) {
[sections addObject:[[MySectionContainter alloc] initWithTitle:section.title andObjects:objects]];
}
}
return sections;
}
- (NSPredicate *)predicateForSearching:(NSString *)text {
return [NSPredicate predicateWithBlock:^BOOL(id _Nullable evaluatedObject, NSDictionary<NSString *,id> * _Nullable bindings) {
if ([evaluatedObject isKindOfClass:[MyClass class]]) {
return [((MyClass *)evaluatedObject).title containsString:text];
}
return NO;
}];
}
示例项目代码:https://pastebin.com/m9V2dunB
截图:
系统行为:
您可以从界面生成器中设置 selection 属性 表视图。 Select XIB 或 Storyboard 中的表格视图,然后 select 属性检查器并设置单个 selectiontoselection` 属性 如下图所示。
删除代码中的这一行:
cell.selectionStyle = UITableViewCellSelectionStyleNone;
我通过将此行添加到 viewDidLoad
来解决您的演示项目中的问题
search.dimsBackgroundDuringPresentation = NO;
只需删除 UISearchController
的暗淡背景即可。
我想在做出选择后关闭控制器,所以这就是我所做的:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.view.endEditing(true)
// 1. Dismiss search controller
searchController.dismiss(animated: false, completion: nil)
let record = fetchedResultsController.object(at: indexPath)
delegate?.didSelectUser(record)
// 2. Dismiss the UITableViewController
dismiss(animated: true, completion: nil)
}
搜索控制器的系统行为:
- 转到 iPhone 设置
- 滑动以显示搜索栏
- 点击搜索栏
- TableView 现在是灰色的,如果我触摸任何地方(不在 searchBar 中),searchBar 将隐藏
- 我输入任何文本,tableView 现在颜色正常,我可以点击任何搜索到的单元格。
在我的应用程序中,点击任何文本后 tableView 仍然是灰色的。我无法点击任何搜索到的单元格。
不同之处还在于,当我搜索时,我会使用 updateSearchResultsForSearchController
更新旧的 tableView(搜索后我没有不同的 tableView),然后再使用 reloadData。
问题是如何在搜索控制器中输入任何文本后隐藏此灰色视图并提供点击单元格的机会?
我创建搜索控制器的方式:
UISearchController * search = [[UISearchController alloc] initWithSearchResultsController:nil];
search.searchResultsUpdater = self;
self.navigationItem.searchController = search;
self.navigationItem.hidesSearchBarWhenScrolling = YES;
self.navigationItem.searchController.searchBar.barTintColor = [UIColor blueColor];
self.navigationItem.searchController.searchBar.delegate = self;
以及我如何更新 tableView 单元格:
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
self.actualSearchController = searchController;
self.filteredData = [self filterContentForSearchText:searchController.searchBar.text];
[self.tableView reloadData];
}
- (NSMutableArray<MySectionContainter *> *)filterContentForSearchText:(NSString *)text {
NSMutableArray<MySectionContainter *> *sections = [NSMutableArray array];
for (MySectionContainter *section in self.tableData) {
NSArray *objects = [sectionInfo.rowObjects filteredArrayUsingPredicate:[self.filterSource predicateForSearching:text]];
if ([objects count] > 0) {
[sections addObject:[[MySectionContainter alloc] initWithTitle:section.title andObjects:objects]];
}
}
return sections;
}
- (NSPredicate *)predicateForSearching:(NSString *)text {
return [NSPredicate predicateWithBlock:^BOOL(id _Nullable evaluatedObject, NSDictionary<NSString *,id> * _Nullable bindings) {
if ([evaluatedObject isKindOfClass:[MyClass class]]) {
return [((MyClass *)evaluatedObject).title containsString:text];
}
return NO;
}];
}
示例项目代码:https://pastebin.com/m9V2dunB
截图:
系统行为:
您可以从界面生成器中设置 selection 属性 表视图。 Select XIB 或 Storyboard 中的表格视图,然后 select 属性检查器并设置单个 selectiontoselection` 属性 如下图所示。
删除代码中的这一行:
cell.selectionStyle = UITableViewCellSelectionStyleNone;
我通过将此行添加到 viewDidLoad
search.dimsBackgroundDuringPresentation = NO;
只需删除 UISearchController
的暗淡背景即可。
我想在做出选择后关闭控制器,所以这就是我所做的:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.view.endEditing(true)
// 1. Dismiss search controller
searchController.dismiss(animated: false, completion: nil)
let record = fetchedResultsController.object(at: indexPath)
delegate?.didSelectUser(record)
// 2. Dismiss the UITableViewController
dismiss(animated: true, completion: nil)
}