xcode tableView.tableHeaderView 正在设置但未绘制
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3425502/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
tableView.tableHeaderView being set but not drawn
提问by quantumpotato
whenever I set my tableHeaderView I'm not seeing it in the Simulator. If I add it as a subview, it ends up getting drawn underneath the section header. Any idea what I'm missing here?
每当我设置 tableHeaderView 时,我都没有在模拟器中看到它。如果我将它添加为子视图,它最终会被绘制在节标题下方。知道我在这里缺少什么吗?
I do have a XIB file. I didn't see any properties in IB to affect headerViews though.
我有一个 XIB 文件。我没有在 IB 中看到任何影响 headerViews 的属性。
- (void)viewDidLoad {
[super viewDidLoad];
MyTitleView *titleView = [[MyTitleView alloc] initWithFrame:CGRectMake(60,0,260,40)];
titleView.label.text = @"My Title";
self.navigationItem.titleView = titleView;
[titleView release];
StandardTableHeaderView *headerView = [[StandardTableHeaderView alloc] initWithFrame:CGRectMake(0,0,320,44)];
self.tableView.tableHeaderView = headerView;
// [self.view addSubview:self.tableView.tableHeaderView];
// [headerView release];
NSLog(@"Header: %@",self.tableView.tableHeaderView); //Logs ] Header: <StandardTableHeaderView: 0x5a508b0; frame = (0 0; 320 44); layer = <CALayer: 0x5a51130>>
Edit: StandardTableHeaderView.m init method:
编辑:StandardTableHeaderView.m init 方法:
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
self.backgroundColor = [UIColor redColor];
self.label = [[UILabel alloc] initWithFrame:CGRectMake(frame.origin.x,0,frame.size.width,frame.size.height)];
self.label.backgroundColor = [UIColor clearColor];
self.label.textColor = [UIColor whiteColor];
self.label.font = [UIFont fontWithName:@"Helvetica" size:16];
[self addSubview:self.label];
}
return self;
}
采纳答案by Michael Kessler
First of all your code looks fine.
首先,您的代码看起来不错。
2 possible problems:
2个可能的问题:
self.tableView
is not settableHeaderView
is overridden afterviewDidLoad
self.tableView
未设置tableHeaderView
之后被覆盖viewDidLoad
回答by Rob
I was having the same problem and noticed that any tableViewHeader that I defined in my NIB would not appear if I initialized my tableview by invoking:
我遇到了同样的问题,并注意到如果我通过调用初始化我的 tableview,我在我的 NIB 中定义的任何 tableViewHeader 都不会出现:
- (id)initWithStyle:(UITableViewStyle)style
But it magically appeared without effort when I initialized my tableview by using:
但是当我使用以下命令初始化我的 tableview 时,它毫不费力地神奇地出现了:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
Weird.
奇怪的。
回答by Kalle
Note! If you expect the table view to set the frame of the header view for you you're mistaken and will get very weird results.
笔记!如果您期望表格视图为您设置标题视图的框架,那么您就错了,并且会得到非常奇怪的结果。
I had mine set to CGRectZero
thinking it'd solve itself, but the thing wouldn't appear and when it did, it would appear tucked in under the navbar or sticking down into the first cell and such.
我让我CGRectZero
认为它会自行解决,但它不会出现,当它出现时,它会出现在导航栏下方或粘在第一个单元格中等等。
回答by Denis Kutlubaev
I tried to put the setup of tableHeaderView
in ViewDidLoad
and loadView
methods - it didn't work: part of the view was white, not visible completely. Then I tried to put it to ViewDidAppear
and it worked:
我试图设置tableHeaderView
inViewDidLoad
和loadView
方法 - 它不起作用:视图的一部分是白色的,不完全可见。然后我尝试将其放入ViewDidAppear
并且它起作用了:
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self setupTableHeaderView];
}
回答by Fonix
I had a problem where i was putting a view inside a tableviewcontroller as a header and it would not show up when i ran the app, the problem was i added the header when there were no prototype cells. if you add at least one prototype cell, then add the header above it, then delete the cell (if need be), it registered it as a headerview properly
我有一个问题,我将一个视图放在 tableviewcontroller 中作为标题,但当我运行应用程序时它不会显示,问题是我在没有原型单元格时添加了标题。如果您添加至少一个原型单元格,然后在其上方添加标题,然后删除单元格(如果需要),它会正确地将其注册为标题视图
回答by swapnali patil
when you UIView inside the UITableView in the storyboard and you try to set tableView.tableHeaderView = YOUR_VIEW, it won't work. You have to first drag-drop that view outside view controller create its outlet and then set
当您在情节提要中的 UITableView 中的 UIView 并尝试设置 tableView.tableHeaderView = YOUR_VIEW 时,它将不起作用。您必须首先将该视图拖放到视图控制器外创建其出口,然后设置
tableView.tableHeaderView = YOUR_VIEW
tableView.tableHeaderView = YOUR_VIEW
回答by chongsj
Try setting UITableViewDelegate in your header and place the following code in your .m file.
尝试在标题中设置 UITableViewDelegate 并将以下代码放入 .m 文件中。
#pragma mark UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 44.0f;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
StandardTableHeaderView *headerView = [[StandardTableHeaderView alloc] initWithFrame:CGRectMake(0,0,320,44)];
return headerView;
}