2013-09-26 70 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

相关问题