Changing the height of a tableHeaderView hides the

2020-06-08 15:53发布

In my UITableView, I have a tableHeaderView that should resize itself according to the content. The resizing works fine, however, the tableHeaderView obscures the first couple of cells. Apparantly changing the frame of the tableHeaderView doesn't reorganize the rest of the view.

I've already tried calling [self.tableView layoutSubViews] and [self.tableView setNeedsLayout], but those don't help either. Also tried giving the tableHeaderView from the Interface Builder an outlet to change the frame on that, but that doesn't reorganize the tableView either.

How can I change the size of the tableHeaderView, without hiding any cells, reorganizing the rest of the tableView?

2条回答
Luminary・发光体
2楼-- · 2020-06-08 16:13

Try setting the tableHeaderView property of the UITableView after you resized your content.

[self.tableView setTableHeaderView:self.myResizedTableHeaderView];

Solved the problem for me. Hope it helps.

查看更多
聊天终结者
3楼-- · 2020-06-08 16:30

I don't know how you created you UITableView, but I tried to create one with a tableHeaderView like you and it's resizing.

I created a class inheriting from UITableViewController

@interface MyTableViewController : UITableViewController {

}

@end

and for the implementation, in the viewDidLoad, I added:

UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];
[header setBackgroundColor:[UIColor redColor]];
self.tableView.tableHeaderView = header;

enter image description here

查看更多
登录 后发表回答