【发布时间】:2017-11-02 10:01:24
【问题描述】:
搜索控制器的系统行为:
- 进入 iPhone 设置
- 滑动显示搜索栏
- 点击搜索栏
- TableView 现在是灰色的,如果我触摸任何地方(不在 searchBar 中),searchBar 就会隐藏
- 我输入任何文本,tableView 现在颜色正常,我可以点击进入任何搜索到的单元格。
在我的应用程序中,点击任何文本后 tableView 仍然是灰色的。我无法点击任何搜索到的单元格。
不同之处还在于,当我搜索时,我使用updateSearchResultsForSearchController 和 reloadData 更新旧的 tableView(搜索后我没有不同的 tableView)。
问题是如何隐藏这个灰色视图并在搜索控制器中输入任何文本后有机会点击单元格?
我创建搜索控制器的方式:
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
截图:
系统行为:
【问题讨论】:
-
@Tubelight 没有任何改变。
-
请分享你的截图,我会澄清你想要什么?
-
@Tubelight 我的情况与第四点相同(在系统搜索设置中),但在放置文本之后也是如此。我会准备并分享截图。
-
@Tubelight 我添加了截图。我需要创建新项目并以相同的方式添加 tableView 和 searchBar。
-
@Tubelight 我也添加了示例代码。
标签: ios objective-c uitableview uisearchcontroller