在搜索模式下从 UITableView 中删除标题

Posted

技术标签:

【中文标题】在搜索模式下从 UITableView 中删除标题【英文标题】:Removing Header Titles from a UITableView in Search Mode 【发布时间】:2012-08-30 16:17:57 【问题描述】:

刚刚注意到我的 UITableView 在搜索模式下出现了一些不受欢迎的行为。这是问题的直观描述,底部是我怀疑的方法实现。

(第 1 步)完整列表 - 好的!


(第 2 步)搜索结果 - 好的!


(第 3 步)深入搜索搜索结果 - 好的!


(第 4 步)导航回搜索结果 - 不行!正在返回所有 Header 索引...


这是我返回所有索引的方法实现:

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

    NSArray *sKeysArray = [[listContent allKeys] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];

    sorted = [sKeysArray sortedArrayUsingComparator:self.mySortBlock];

    if (tableView == self.searchDisplayController.searchResultsTableView)

        return @"";

    else 

    return [sorted objectAtIndex:section];
  


解决此问题的最佳做法是什么?

谢谢

【问题讨论】:

【参考方案1】:

如果在viewWillAppear:viewDidAppear: 中调用[self.tableView reloadData],我观察到了这种行为。

似乎reloadData 会导致基础表视图的所有部分标题出现,即使搜索处于活动状态并且只有searchResultsTableView 应该可见。

解决方案可能是仅在[self.searchDisplayController isActive] == NO 时调用reloadData

【讨论】:

谢谢,马丁。你为我节省了大量的调试时间!那行得通。

以上是关于在搜索模式下从 UITableView 中删除标题的主要内容,如果未能解决你的问题,请参考以下文章

在编辑模式下按下删除按钮时 UITableView 崩溃

Android Studio AssertionError:不要在哑模式下从内部读取操作调用 waitForSmartMode

UITableView 在删除一行或多行后留下空白单元格的模式

多选模式下的 UITableView 删除选定单元格中所有子视图的背景颜色

允许 UITableView 重新排序,但在编辑模式下不删除,并启用滑动删除

在 UITableview 的编辑模式下滚动时删除按钮消失