0

i have a table view which has header for the tableview and another header for section. the header for section has a button on top of it, when the button is pressed i need to change the size of this header.

i did changed the header size but the content within it does not change accordingly.

i even fixed it using this-

_isHeaderExtended = !_isHeaderExtended;
[self.testTable beginUpdates];
CGPoint point = testTable.contentOffset;
point.y = (_isHeaderExtended)? point.y - 1: point.y + 1;

[testTable setContentOffset:point animated:NO];
[self.testTable endUpdates];

my entire code is below

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{

    return 30;
}

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

    return testView;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {

    [self.testTable beginUpdates];
    CGFloat fl = (_isHeaderExtended)?200:100;
    [self.testTable endUpdates];

    return fl;
}

-(IBAction)buttonPushed:(id)sender {
    _isHeaderExtended = !_isHeaderExtended;

    [self.testTable beginUpdates];
    CGPoint point = testTable.contentOffset;
    point.y = (_isHeaderExtended)? point.y - 1: point.y + 1;

    [testTable setContentOffset:point animated:NO];
    [self.testTable endUpdates];


}

basically i need the content of the header to stretch according to the resize i use, any ideas ???

Mundi
  • 79,884
  • 17
  • 117
  • 140
Yaron Y
  • 105
  • 7
  • Hey, I know this is a old question. But I am facing a similar issue. Could you give me an idea how you have resolved it? – esh May 21 '15 at 13:12

2 Answers2

0

Do this:

  • Eliminate the calls to beginUpdate and endUpdate in heightForHeaderInSection. They have no business in the datasource protocol.

  • Make sure the view you return in the header view is initialized and configured as intended. Presumably you do this in viewDidLoad.

  • In order to let the content of the header stretch, you can set this view's autoresizingMask to the desired value. Alternatively, you can use IB to configure the views to resize with the container view. You will have to return a properly sized view in viewForHeaderInSection.

A recommended design pattern would be quite different: put all the logic of the size into your datasource methods (heightForHeaderInSection and viewForHeaderInSection), i.e. you could check your _isHeaderExpanded variable. Then, in the toggle method, just reload the section.

Mundi
  • 79,884
  • 17
  • 117
  • 140
  • well i did elimante the begin+end in the heightforheaderinsection... the view is obtained from the XIB file so it is handled well i guess. i did used autoresizemask this thing is that the section header does grows to the desired value but it is as if the cells move with animation and the section header is not but it grows and shrinks as i want but the view inside the section does not updates - there are some of the view that is half blue and the other is white, if i move the screen it is updated good BUT i need it to happens without any user UI – Yaron Y Dec 15 '12 at 19:38
  • Then just call `[headerView setNeedsDisplay];` and it should update. – Mundi Dec 15 '12 at 20:48
  • it does not work... the setNeedsDisplay does not help :( it does grows and decrease the size well BUT still the view itself does not updates. any other ideas? i found out that if i move the content by 0.01 it does the trick but there has to be a better way then this improvised way... – Yaron Y Dec 17 '12 at 08:54
  • You can set the size of your views inside the cell explicitly in `willLayoutSubviews` which you can override. I use this technique often and it is very robust. – Mundi Dec 17 '12 at 08:56
0

I think it should be changed into:

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if( _isHeaderExtended ){
        return bigTestView;
    } else {
        return smallTestView;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return _isHeaderExtended?200:100;
}

-(IBAction)buttonPushed:(id)sender {
    _isHeaderExtended = !_isHeaderExtended;
    [self.testTable reloadData];
}

Then you need to create a bigTestView and a smallTestView for the header.

Roland Keesom
  • 8,180
  • 5
  • 45
  • 52