触摸取消时,将UISearchbar隐藏为UITableview中的标题

时间:2010-08-01 04:22:03

标签: uisearchbar uisearchdisplaycontroller

我已将UISearchDisplayController集成到我的控制器的xib中。我要使用的基本功能是在表格的标题中设置搜索栏,在导航栏中显示一个显示搜索显示的按钮。

我坚持的内容:当触摸取消时,我想让搜索栏不可见(保留表格,使索引0显示在顶部,而不是搜索栏),但它保持可见,我不是确定为什么(见第3张图片)。任何想法如何在触摸取消按钮时始终隐藏搜索栏(见图1)。

我尝试了什么:

  1. 将tableview contentOffset设置为44.最初有效。
  2. 调用[tableview scrollToRowAtIndexPath:....],似乎什么也没做。

5 个答案:

答案 0 :(得分: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];
}

答案 1 :(得分:2)

如果您已将搜索栏声明为类似

的属性

@property(nonatomic, retain)UISearchBar *searchBar;

你可以设置

tableView.tableheader = nil;

并将其重新设定

tableView.tableHeader = searchBar;

当你再次需要时。

答案 2 :(得分:1)

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

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


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

答案 3 :(得分:0)

要隐藏UISearchDisplayController的搜索栏,您必须将其设置为非活动状态。这将重新显示导航栏

在您的情况下,您必须确保采用UISearchBarDelegate协议,然后:

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

答案 4 :(得分:0)

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

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

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

   //reload you table here
}

由于