14

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?

SpacyRicochet
  • 2,214
  • 2
  • 22
  • 39
  • 1
    try to input `[myView setClipsToBounds:YES];`in your `viewForheaderInSection`then you can see how much size you view takes – Seega Mar 30 '11 at 09:42

2 Answers2

23

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.

Raginmari
  • 1,575
  • 15
  • 18
  • 3
    specifically for me, I deferred sizing stuff until viewWillAppear: since my controller was dynamically instantiated, and therefore had no proper geometry to reference until then. – qix Aug 09 '12 at 07:27
4

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

Ludovic Landry
  • 10,946
  • 9
  • 45
  • 79
  • I created the TableView through a TableViewController with a nib-file. In there I added a view for the tableHeaderView and inserted a label which contains the content. I change the content and the label frame by adjusting an IBOutlet. But doing the same with the tableHeaderView IBOutlet, doesn't reorganize the tableview. – SpacyRicochet Mar 30 '11 at 14:33
  • 2
    In the end, I used this method, without creating the view in Interface Builder. Thanks! – SpacyRicochet Aug 09 '11 at 07:33
  • Hey! Why is it that I add the same lines to viewDidLoad and I can't see the red block above?! – János Mar 11 '12 at 07:57
  • Good answer but it does not solve the problem if you do not inherit from an UITableViewController. – Chris Mar 20 '14 at 21:23
  • So weird issue :( spent a day to figure it out. Using tableHeaderView in viewController + tableView with dynamic content is buggy. tableHeaderView is overlapping cells, blocking user interaction, didSelectRow and so on... Switching from UIViewController + tableView to UITableViewController did the trick for me :) thanks a lot ! – polo987 Jan 09 '16 at 12:10