问题是UITableView有一些单元格,它们的“附件类型”设置为UITableViewCellAccessoryDetailDisclosureButton.发生这种情况:
开始,一切看起来应该是:
>用户点击UISearchBar.
> UISearchDisplayController在主表的顶部创建黑色叠加层,并使主表的索引(as,sectionIndexTitlesForTableView)消失.
>假设用户此时隐藏键盘(按下标准键盘上的iPad“隐藏键盘”按钮)
>由于用户还没有在UISearchBar中输入任何内容,我们仍然可以看到主表,尽管在UISearchDisplayController添加的黑色叠加层之下.
>键盘的隐藏暴露了更多的主表,导致主表加载更多的单元格.
>现在这里的问题是:由于这些单元格在主表的索引被隐藏时加载,所以显示的按钮显示得太过分(至少与其他单元格相比).
>此外,当用户现在取消搜索时,可能不会重新加载这些单元,导致公开按钮被显示在索引下面(现在再次可见).
我正在努力解决这个问题;我可以想到的唯一选择是找到对应于公开按钮的UIView,并手动移动它,但是这似乎是非常可笑的,如果只是因为甚至找到UIView requires a nasty hack.关于如何以更好的方式解决这个问题的任何建议非常感谢!
最小的可运行示例
以下是一个最小的例子.只需启动一个新的XCode项目,仅启用ARC,iPad,并将AppDelegate的内容替换为以下内容.请注意,为了最小化的示例,我强制主表在searchDisplayController中重新加载单元:willShowSearchResultsTableView,否则主表将缓存其单元格,并且问题不会显示(在我的实际应用程序中,主表正在重新加载其单元格对于其他原因,我不完全确定为什么 – 但是当然,主表可以随时重新加载单元格.)
要查看问题发生,请运行代码,在搜索框中输入内容(您将看到“搜索结果0 .. 5”),然后取消搜索.主表的公开按钮现在显示在索引下方而不是旁边.
下面只是代码:
- @interface AppDelegate ()
- @property (nonatomic,strong) UITableViewController* mainTableController;
- @property (nonatomic,strong) UISearchDisplayController* searchDisplay;
- @end
- @implementation AppDelegate
- - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
- {
- self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
- // Override point for customization after application launch.
- UITableViewController* tableViewController = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
- UITableView* tableView = [tableViewController tableView];
- [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
- [tableView setDataSource:self];
- [self setMainTableController:tableViewController];
- UISearchBar* searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0,44)]; // Width set automatically
- [tableView setTableHeaderView:searchBar];
- UISearchDisplayController* searchDisplay = [[UISearchDisplayController alloc] initWithSearchBar:searchBar
- contentsController:tableViewController];
- [searchDisplay setSearchResultsDataSource:self];
- [searchDisplay setDelegate:self];
- [self setSearchDisplay:searchDisplay];
- [[self window] setRootViewController:tableViewController];
- self.window.backgroundColor = [UIColor whiteColor];
- [self.window makeKeyAndVisible];
- return YES;
- }
- #pragma mark Table view data source
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
- if (tableView != [[self searchDisplay] searchResultsTableView]) {
- return 26;
- } else {
- return 1;
- }
- }
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
- if (tableView != [[self searchDisplay] searchResultsTableView]) {
- return 10;
- } else {
- return 5;
- }
- }
- - (void)searchDisplayController:(UISearchDisplayController *)controller willShowSearchResultsTableView:(UITableView *)tableView {
- // The problem arises only if the main table view needs to reload its data
- // In this minimal example,we force this to happen
- [[[self mainTableController] tableView] reloadData];
- [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"SearchCell"];
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
- if (tableView != [[self searchDisplay] searchResultsTableView]) {
- UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
- [[cell textLabel] setText:[NSString stringWithFormat:@"%c%d",'A' + [indexPath section],[indexPath row]]];
- [cell setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton];
- return cell;
- } else {
- UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"SearchCell" forIndexPath:indexPath];
- [[cell textLabel] setText:[NSString stringWithFormat:@"Search result %d",[indexPath row]]];
- [cell setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton];
- return cell;
- }
- }
- - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
- if (tableView != [[self searchDisplay] searchResultsTableView]) {
- return [NSArray arrayWithObjects:@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K",@"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z",nil];
- } else {
- return nil;
- }
- }
解决方法
我在你的例子中已经实现了这一点,通过在主表中查看两个UISearchDisplayDelegate方法,我添加到你的代码中调用reloadData:
- - (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller
- {
- [[[self mainTableController] tableView] reloadData];
- }
- - (void)searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller
- {
- [[[self mainTableController] tableView] reloadData];
- }
这将强制您的详细披露视图重新定位,以考虑表视图索引的可见性,保持所有披露指标的位置彼此一致,无论是否在搜索模式.
更新
我在其他UISearchDisplayDelegate方法中重新加载表视图,包括:
- - (void)searchDisplayController:(UISearchDisplayController *)controller didLoadSearchResultsTableView:(UITableView *)tableView
- - (void)searchDisplayController:(UISearchDisplayController *)controller willUnloadSearchResultsTableView:(UITableView *)tableView
但是这些产生了一个相当刺耳的效果,其中详细的公开按钮的位置突然跳出来,所以我建议如前所述的willBeginSearch和willEndSearch方法.