min*_*omb 4 objective-c uitableview ios ios7
如何将一个简单的UILabel添加到TableHeaderView并保留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];
Run Code Online (Sandbox Code Playgroud)
添加到UITableViewController viewDidLoad事件(包含一个UISearchDisplayerController)在iOS6中给出了这个可爱的结果:

这个在iOS7中可怕的糟糕结果:

行为: 在正常模式下,我添加的UILabel未显示.当搜索处于活动状态时,UILabels突然出现在表格单元格的顶部,并且不会滚动
此外,我在iOS 7中搜索时遇到崩溃,这在iOS6上从未发生过.可能与这段代码没有关系,但我应该提到这一点.
我尝试了所有我能找到的关于解决这个问题的东西,但总是其他东西破坏或消失(主要是UISearchBar).
救命
看看伙计,在修改代码或对iOS7的行为感到沮丧之前,建议您浏览一下apple 提供的UI Transition Guide.
阅读本文之后,您将了解到,为什么分组样式tableView看起来很烦人,从ios7的角度来看并不烦人.每组扩展屏幕的整个宽度.
此外,UISearchBar如果您通过搜索栏和范围栏阅读,则可以控制行为
我希望有所帮助.如果您需要所有代码,请告诉我们.我们可以提供样品.
这是示例代码为必需 将UISearchBar添加到View而不是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];
Run Code Online (Sandbox Code Playgroud)添加UITableView到self.view
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];
Run Code Online (Sandbox Code Playgroud)创建和添加 UITableView.tableHeaderView
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;
Run Code Online (Sandbox Code Playgroud)结果如下:


SearchDisplayController
// Create search controller
searchController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
searchController.searchResultsDataSource = self;
searchController.searchResultsDelegate = self;
searchController.delegate = self;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5770 次 |
| 最近记录: |