在搜索过程中,ios – UITableViewCell的详细信息按钮按钮太远

前端之家收集整理的这篇文章主要介绍了在搜索过程中,ios – UITableViewCell的详细信息按钮按钮太远前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有UITableViewController与一个UISearchBar作为tableView的tableHeaderView.我也有一个UISearchDisplayController初始化为UISearchBar作为其searchBar和UITableViewController作为其内容控制器.到目前为止这么好,一切都有效.

问题是UITableView有一些单元格,它们的“附件类型”设置为UITableViewCellAccessoryDe​​tailDisclosureButton.发生这种情况:

开始,一切看起来应该是:
>用户点击UISearchBar.
> UISearchDisplayController在主表的顶部创建黑色叠加层,并使主表的索引(as,sectionIndexTitlesForTableView)消失.
>假设用户此时隐藏键盘(按下标准键盘上的iPad“隐藏键盘”按钮)
>由于用户还没有在UISearchBar中输入任何内容,我们仍然可以看到主表,尽管在UISearchDisplayController添加的黑色叠加层之下.
>键盘的隐藏暴露了更多的主表,导致主表加载更多的单元格.
>现在这里的问题是:由于这些单元格在主表的索引被隐藏时加载,所以显示的按钮显示得太过分(至少与其他单元格相比).
>此外,当用户现在取消搜索时,可能不会重新加载这些单元,导致公开按钮被显示在索引下面(现在再次可见).

我正在努力解决这个问题;我可以想到的唯一选择是找到对应于公开按钮的UIView,并手动移动它,但是这似乎是非常可笑的,如果只是因为甚至找到UIView requires a nasty hack.关于如何以更好的方式解决这个问题的任何建议非常感谢!

最小的可运行示例

以下是一个最小的例子.只需启动一个新的XCode项目,仅启用ARC,iPad,并将AppDelegate的内容替换为以下内容.请注意,为了最小化的示例,我强制主表在searchDisplayController中重新加载单元:willShowSearchResultsTableView,否则主表将缓存其单元格,并且问题不会显示(在我的实际应用程序中,主表正在重新加载其单元格对于其他原因,我不完全确定为什么 – 但是当然,主表可以随时重新加载单元格.)

要查看问题发生,请运行代码,在搜索框中输入内容(您将看到“搜索结果0 .. 5”),然后取消搜索.主表的公开按钮现在显示在索引下方而不是旁边.

下面只是代码

  1. @interface AppDelegate ()
  2.  
  3. @property (nonatomic,strong) UITableViewController* mainTableController;
  4. @property (nonatomic,strong) UISearchDisplayController* searchDisplay;
  5.  
  6. @end
  7.  
  8. @implementation AppDelegate
  9.  
  10. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  11. {
  12. self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  13.  
  14. // Override point for customization after application launch.
  15.  
  16. UITableViewController* tableViewController = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
  17.  
  18. UITableView* tableView = [tableViewController tableView];
  19. [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
  20. [tableView setDataSource:self];
  21. [self setMainTableController:tableViewController];
  22.  
  23. UISearchBar* searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0,44)]; // Width set automatically
  24. [tableView setTableHeaderView:searchBar];
  25.  
  26. UISearchDisplayController* searchDisplay = [[UISearchDisplayController alloc] initWithSearchBar:searchBar
  27. contentsController:tableViewController];
  28. [searchDisplay setSearchResultsDataSource:self];
  29. [searchDisplay setDelegate:self];
  30. [self setSearchDisplay:searchDisplay];
  31.  
  32. [[self window] setRootViewController:tableViewController];
  33.  
  34. self.window.backgroundColor = [UIColor whiteColor];
  35. [self.window makeKeyAndVisible];
  36. return YES;
  37. }
  38.  
  39. #pragma mark Table view data source
  40.  
  41. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  42. if (tableView != [[self searchDisplay] searchResultsTableView]) {
  43. return 26;
  44. } else {
  45. return 1;
  46. }
  47. }
  48.  
  49. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  50. if (tableView != [[self searchDisplay] searchResultsTableView]) {
  51. return 10;
  52. } else {
  53. return 5;
  54. }
  55. }
  56.  
  57. - (void)searchDisplayController:(UISearchDisplayController *)controller willShowSearchResultsTableView:(UITableView *)tableView {
  58. // The problem arises only if the main table view needs to reload its data
  59. // In this minimal example,we force this to happen
  60. [[[self mainTableController] tableView] reloadData];
  61.  
  62. [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"SearchCell"];
  63. }
  64.  
  65. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  66. if (tableView != [[self searchDisplay] searchResultsTableView]) {
  67. UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
  68.  
  69. [[cell textLabel] setText:[NSString stringWithFormat:@"%c%d",'A' + [indexPath section],[indexPath row]]];
  70. [cell setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton];
  71.  
  72. return cell;
  73. } else {
  74. UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"SearchCell" forIndexPath:indexPath];
  75.  
  76. [[cell textLabel] setText:[NSString stringWithFormat:@"Search result %d",[indexPath row]]];
  77. [cell setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton];
  78.  
  79. return cell;
  80. }
  81. }
  82.  
  83. - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
  84. if (tableView != [[self searchDisplay] searchResultsTableView]) {
  85. 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];
  86. } else {
  87. return nil;
  88. }
  89. }

解决方法

如果您想要模仿苹果自己的应用程序在这些情况下似乎表现的方式,那么正确的操作过程将是开始搜索时所有向右移动的详细信息按钮的原因,然后再次重新移动一次搜索完成.

我在你的例子中已经实现了这一点,通过在主表中查看两个UISearchDisplayDelegate方法,我添加到你的代码调用reloadData:

  1. - (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller
  2. {
  3. [[[self mainTableController] tableView] reloadData];
  4. }
  5.  
  6. - (void)searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller
  7. {
  8. [[[self mainTableController] tableView] reloadData];
  9. }

这将强制您的详细披露视图重新定位,以考虑表视图索引的可见性,保持所有披露指标的位置彼此一致,无论是否在搜索模式.

更新

我在其他UISearchDisplayDelegate方法中重新加载表视图,包括

  1. - (void)searchDisplayController:(UISearchDisplayController *)controller didLoadSearchResultsTableView:(UITableView *)tableView
  2. - (void)searchDisplayController:(UISearchDisplayController *)controller willUnloadSearchResultsTableView:(UITableView *)tableView

但是这些产生了一个相当刺耳的效果,其中详细的公开按钮的位置突然跳出来,所以我建议如前所述的willBeginSearch和willEndSearch方法.

猜你在找的iOS相关文章