在iOS7中使用UISearchBar将子视图添加到tableHeaderView会出现错误行为

在iOS7中使用UISearchBar将子视图添加到tableHeaderView会出现错误行为,ios,objective-c,ios7,uitableview,Ios,Objective C,Ios7,Uitableview,如何向TableHeaderView添加两个简单的UILabel,并在iOS7中保留搜索显示控制器的默认搜索栏行为 或者,视觉上: 为什么该代码: UIView *tableHeadView = self.tableView.tableHeaderView; UILabel *tableHeaderLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 36, 320, 30)]; [tableHeaderLabel setTextAli

如何向TableHeaderView添加两个简单的UILabel,并在iOS7中保留搜索显示控制器的默认搜索栏行为

或者,视觉上:

为什么该代码:

UIView *tableHeadView = self.tableView.tableHeaderView;   

UILabel *tableHeaderLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 36, 320, 30)];
[tableHeaderLabel setTextAlignment:NSTextAlignmentCenter];
tableHeaderLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:18];
tableHeaderLabel.text = @"Questions"

UILabel *tableHeaderPrononce = [[UILabel alloc] initWithFrame:CGRectMake(0, 50, 320, 30)];
[tableHeaderPrononce setTextAlignment:NSTextAlignmentCenter];
tableHeaderPrononce.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:15];
tableHeaderPrononce.text = @"test test test test";

[tableHeadView addSubview:tableHeaderLabel];
[tableHeadView addSubview:tableHeaderPrononce];
添加到UITableViewController viewDidLoad事件(其中包含UISearchDisplayController) 在iOS6中给出了这个可爱的结果:


而这一可怕的结果将导致iOS7:

行为: 在正常模式下,不会显示我添加的UIL标签。当搜索处于活动状态时,UILabel会突然出现在表格单元格的顶部,并且不会滚动

此外,我在iOS 7中搜索时遇到了在iOS 6上从未发生过的崩溃。可能与这段代码无关,但我应该提到这一点

我尝试了所有我能找到的关于修复这个问题的方法,但总是有其他东西出现或消失(主要是UISearchBar)


帮助

看一看,伙计,在你修改代码或对iOS7的行为感到沮丧之前,建议你先看一下苹果给出的答案

阅读本文后,您将了解到,为什么分组样式的tableView看起来令人讨厌,从ios7的角度来看,这一点也不令人讨厌每组扩展屏幕的全宽。

此外,如果通读,
ui搜索栏
的行为可以控制

我希望这有帮助。如果您需要所有的代码,请让我们知道。我们可以提供样品

以下是所需的示例代码将UISearchBar添加到视图而不是TableViewHeader

  • UISearchBar
    添加到
    self.view

    searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0.0f,0.0f, 320, 44.0f)];
    searchBar.delegate         = self;
    searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    searchBar.searchBarStyle = UISearchBarStyleMinimal;
    [self.view addSubview:searchBar];
    
    tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 44, self.view.bounds.size.width, self.view.bounds.size.height) style:UITableViewStylePlain];
    tableView.delegate         = self;
    tableView.dataSource       = self;
    tableView.backgroundColor = [UIColor clearColor];
    tableView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
    [self.view addSubview:tableView];
    
    UILabel *tableHeaderLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 15)];
    [tableHeaderLabel setTextAlignment:NSTextAlignmentCenter];
    tableHeaderLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:18];
    tableHeaderLabel.text = @"Questions";
    tableHeaderLabel.alpha = 0.9;
    
    UILabel *tableHeaderPrononce = [[UILabel alloc] initWithFrame:CGRectMake(0, 14, 320, 15)];
    [tableHeaderPrononce setTextAlignment:NSTextAlignmentCenter];
    tableHeaderPrononce.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:15];
    tableHeaderPrononce.text = @"test test test test";
    tableHeaderPrononce.alpha = 0.7;
    
    UIView *aHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 30)];
    [aHeaderView setBackgroundColor:[UIColor clearColor]];
    [aHeaderView addSubview:tableHeaderLabel];
    [aHeaderView addSubview:tableHeaderPrononce];
    tableView.tableHeaderView = aHeaderView;
    
  • UITableView
    添加到
    self.view

    searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0.0f,0.0f, 320, 44.0f)];
    searchBar.delegate         = self;
    searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    searchBar.searchBarStyle = UISearchBarStyleMinimal;
    [self.view addSubview:searchBar];
    
    tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 44, self.view.bounds.size.width, self.view.bounds.size.height) style:UITableViewStylePlain];
    tableView.delegate         = self;
    tableView.dataSource       = self;
    tableView.backgroundColor = [UIColor clearColor];
    tableView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
    [self.view addSubview:tableView];
    
    UILabel *tableHeaderLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 15)];
    [tableHeaderLabel setTextAlignment:NSTextAlignmentCenter];
    tableHeaderLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:18];
    tableHeaderLabel.text = @"Questions";
    tableHeaderLabel.alpha = 0.9;
    
    UILabel *tableHeaderPrononce = [[UILabel alloc] initWithFrame:CGRectMake(0, 14, 320, 15)];
    [tableHeaderPrononce setTextAlignment:NSTextAlignmentCenter];
    tableHeaderPrononce.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:15];
    tableHeaderPrononce.text = @"test test test test";
    tableHeaderPrononce.alpha = 0.7;
    
    UIView *aHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 30)];
    [aHeaderView setBackgroundColor:[UIColor clearColor]];
    [aHeaderView addSubview:tableHeaderLabel];
    [aHeaderView addSubview:tableHeaderPrononce];
    tableView.tableHeaderView = aHeaderView;
    
  • 创建和添加
    UITableView.tableHeaderView

    searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0.0f,0.0f, 320, 44.0f)];
    searchBar.delegate         = self;
    searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    searchBar.searchBarStyle = UISearchBarStyleMinimal;
    [self.view addSubview:searchBar];
    
    tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 44, self.view.bounds.size.width, self.view.bounds.size.height) style:UITableViewStylePlain];
    tableView.delegate         = self;
    tableView.dataSource       = self;
    tableView.backgroundColor = [UIColor clearColor];
    tableView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
    [self.view addSubview:tableView];
    
    UILabel *tableHeaderLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 15)];
    [tableHeaderLabel setTextAlignment:NSTextAlignmentCenter];
    tableHeaderLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:18];
    tableHeaderLabel.text = @"Questions";
    tableHeaderLabel.alpha = 0.9;
    
    UILabel *tableHeaderPrononce = [[UILabel alloc] initWithFrame:CGRectMake(0, 14, 320, 15)];
    [tableHeaderPrononce setTextAlignment:NSTextAlignmentCenter];
    tableHeaderPrononce.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:15];
    tableHeaderPrononce.text = @"test test test test";
    tableHeaderPrononce.alpha = 0.7;
    
    UIView *aHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 30)];
    [aHeaderView setBackgroundColor:[UIColor clearColor]];
    [aHeaderView addSubview:tableHeaderLabel];
    [aHeaderView addSubview:tableHeaderPrononce];
    tableView.tableHeaderView = aHeaderView;
    
结果如下:

SearchDisplayController


只需使用如下所示的UITableViewDelegate方法即可完成此操作:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

    UIView *sectionHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 40)];
    sectionHeaderView.backgroundColor = [UIColor grayColor];
    UILabel *headerLabel = [[UILabel alloc] init];
    [sectionHeaderView addSubview:headerLabel];

    return sectionHeaderView;
}

看来你最好需要代码。过段时间我会给你密码。谢谢。看我的编辑。如果您希望将UISearchBar添加为tableHeaderView的一部分,我将添加更多代码。好的,很酷,谢谢!问题是,您已经创建了一个独立的UISearchBar,其中我的链接到UISearchDisplayController。此外,如果我没有添加这两个UILabel,它已经出现在TableViewHeader上。不,它链接到UISearchDisplayController。由于该代码不需要显示,因此我在此未提及。请允许我在手机上以最少的互联网接入进行操作时,预计我的回复会延迟-(如果有任何方法可以链接到github上的项目,我会为您查看这两个问题。它不在git上,但所有相关代码都在这里