2010-08-01 49 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 
} 

感謝