UITableView标头不显示autolayout tableview

时间:2015-03-31 12:10:35

标签: ios uitableview autolayout

我正在使用autolayout将uitableview添加到uiviewcontroller。

在viewDidLoad

[self.view addSubview:self.tableView];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[tableView]|" options:0 metrics:nil views:views]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[tableView|" options:0 metrics:nil views:views]];

我想在ViewDidLoad

中添加一个tableviewheader
UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, _tableView.frame.size.width, 200)];
self.tableView.tableHeaderView = header;

但由于tableview未在viewDidLoad中呈现,因此其宽度为0,因此标题永远不会显示。

如何设置标题的框架以便显示?

2 个答案:

答案 0 :(得分:0)

尝试使用

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

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

设置表格视图的标题。

答案 1 :(得分:0)

添加约束后,您可以调用[self.view layoutIfNeeded]。在此之后,tableView的框架应该是正确的框架,因此创建headerView框架应该可行。

如果您在headerView中有子视图,那么它们也应该有约束,而不是静态帧。

让我知道它是怎么回事,或者你是否需要更多的帮助。