2010-08-01 39 views
1

我已将UISearchDisplayController集成到我的控制器的xib中。我要做的基本功能是在表格的标题中包含搜索栏,并在导航栏中显示搜索显示的按钮。当取消触摸时,将UISearchbar作为标题隐藏在UITableview中

什么我卡住了:当取消被触摸时,我想使搜索栏不可见(保持表格的索引0显示在顶部,而不是搜索栏),但它保持可见,我是不知道为什么(见第三张图片)。任何想法如何在取消按钮被触摸时始终隐藏搜索栏(见图1)。

我已经试过什么:

  1. 设置的tableview contentOffset至44其中一期工程开始。
  2. 调用[tableview scrollToRowAtIndexPath:....],它似乎没有做任何事情。

回答

2

试试这个:

- (void)viewWillAppeared:(BOOL)animated 
{ 
     [self hidesSeachBar]; 
     [super viewWillAppeared:animated]; 
} 

- (void)hidesSearchBar 
{ 
     CGSize searchSize = self.searchDisplayController.searchBar.bounds.size; 
     //not complete 
     [self.tableView setContentOffset:CGPointMake(0, searchSize.height)]; 
} 

- (void) searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller 
{ 
    //Your code 

    [self.tableView reloadData]; 
    [self hidesSearchBar]; 
} 
+0

这个工作很适合我。我唯一的建议是将该方法命名为“hideSearchBar”(而不是“hidesSearchBar”),因为它是一个命令,而不是查询布尔值。 – Giovanni 2013-01-01 07:47:31

0

躲UISearchDisplayController的搜索栏,你必须将其设置为无效。这将重新显示在导航栏以及

在你的情况,你必须确保你采取UISearchBarDelegate协议,然后:

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar 
{ 
    [self.searchDisplayController setActive:NO animated:YES]; 
} 
+0

这只是使搜索栏处于非活动状态,即与您按取消按钮相同。而问题是如何将其隐藏在tableview标题中。 – 2016-10-06 08:21:25

1

设置内容偏移来隐藏它的方式,你需要在搜索结束时执行此操作。我使用的是这样的:

- (void) searchDisplayControllerWillEndSearch:(UISearchDisplayController*)controller 
{ 
    [self hideSearchBar]; 
} 


- (void) hideSearchBar 
{ 
    self.table.contentOffset = CGPointMake(0, self.searchBar.frame.size.height); 
} 
2

如果你已经声明为像

@property(nonatomic, retain)UISearchBar *searchBar;

属性搜索栏可以设置

tableView.tableheader = nil;

和将其设置回

tableView.tableHeader = searchBar;

当你需要它时。

0

要隐藏取消搜索栏,你需要调用UISearchBarDelegate方法:

(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar 
{ 
    if(searchTask)[searchTask cancel]; 

    [self.searchResults removeAllObjects]; 
    [self.searchDisplayController.searchResultsTableView reloadData]; 

    //reload you table here 
} 

感谢