2013-09-26 69 views
4

我有一個基於tabbar的應用程序,並且UInavigationcontroller爲每個選項卡。在TabViewController中,我實現了UIsegmentedcontrol,searchDisplayController和uitableview。 navigationItems,tabledata根據分段控制選擇進行更改。對於我隱藏搜索欄的細分受衆羣。但是,當搜索欄被隱藏時,tableview第一行不響應didselectrowatindexpath隱藏UISearchDisplayController的UISearchBar

這裏是我的代碼,

在段變化的行動

- (void)indexDidChangeForSegmentedControl:(UISegmentedControl *)aSegmentedControl { 
[self changeNavigationItems]; 

l.text = [NSString stringWithFormat:@"%d",self.segmentControl.selectedSegmentIndex]; 
if([segmentIndexesToHideSearchBar containsObject: [NSString stringWithFormat:@"%d", self.segmentControl.selectedSegmentIndex]]) 
{ 
    self.searchDisplayController.searchBar.hidden = YES; 
    self.dataTable.frame = CGRectMake(0, 0, self.dataTable.frame.size.width, self.dataTable.frame.size.height); 
} 
else 
{ 
    self.searchDisplayController.searchBar.hidden = NO; 
    self.dataTable.frame = CGRectMake(0, 44, self.dataTable.frame.size.width, self.dataTable.frame.size.height); 
} 
[self.dataTable reloadData]; 

}

其他代碼是通用的,其他的事情正在正確的。

第二個問題是,當我通過單擊某一行從細節視圖返回時,表格框架的更改未保留。搜索欄有一個空間。

等待幫助。

回答

1

我已經想通了。我的第一個問題是第一次點擊tableview行沒有迴應。那是因爲我錯誤didSelectRowAtIndexPathdidDeselectRowAtIndexPath。多麼愚蠢的錯誤,我忍受了幾個小時...... :(

第二個問題是因爲我在viewDidLoad函數中編寫了隱藏和幀改變代碼,我將代碼移到了viewDidAppear函數中。 。

6

我想這是不正確的做法,但它爲我工作:) 使其隱藏:

CGRect searchFrame = self.searchDisplayController.searchBar.frame; 
searchFrame.size.height = 0; 

self.searchDisplayController.searchBar.frame = searchFrame; 
self.searchDisplayController.searchBar.hidden = YES; 

要再次「兜底」吧:

searchFrame.size.height = 44; 
self.searchDisplayController.searchBar.frame = searchFrame; 
self.searchDisplayController.searchBar.hidden = NO; 

我不知道這是否適用於自動佈局,從來沒有嘗試過。 (這也是在Xcode < 5,iOS < 7)

+0

謝謝你的時間,但我的代碼完全隱藏了搜索欄。我在其他地方遇到問題。第一次單擊不響應表格單元格,當tableview返回時,它不會在搜索欄隱藏後保留更改的框架。 – tausun

+0

哦,我看到你的答案:)這樣的事情總是發生在我身上:D,我很高興你明白了,並感謝upvote :) – Yanchi

+0

不是一個不好的解決方案。我對「顯示」代碼中44個硬編碼假設有點謹慎。但是,「隱藏」代碼在iOS 7中工作得很好。 – Mark

相關問題