有一个带索引的 TableView 。它们在初始加载时可见。
我还有一个 searchDisplayController
当我使用 searchDisplayController 然后取消它时,突然隐藏了原始 tableview 上的索引。
我在 iOS 6 上从来没有遇到过这个问题。
这是我在 iOS 6 上运行的代码:
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView2 {
if (self.searchDisplayController.active)
return nil;
else
return self.indices;
}
我已经试过了,但没有成功:
- (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller {
[self.tableViewOriginal reloadSectionIndexTitles];
}
- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller {
[self.tableViewOriginal reloadSectionIndexTitles];
}
更新:
为了添加 tableView,我使用了 Storyboard并将其与 IBOutlet 连接。 searchBar 和 searchDisplayController 以编程方式添加到 viewDidLoad 中:
self.searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, 290, 44)];
self.searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self];
如果 cellForRowAtIndexPath,我然后将搜索栏添加到表格单元格
[cell addSubview:self.searchBar];
最佳答案
您的设置的一个可能问题是您将搜索栏放在表格 View 单元格中。
一个更标准(也更简单)的方法是让搜索栏成为表格 View 的 headerView
。
self.tableView.headerView = self.searchBar;
只需在设置搜索显示 Controller 后执行一次即可。
关于iOS7 searchDisplayController 隐藏 tableview 索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23696855/