Search code examples
iphonexcodeuitableviewiphone-developer-programsectionheader

UITableView resize header for section does not stretch according to its size


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 ???


Solution

  • 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.