Pergunta

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

Foi útil?

Solução

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.

Outras dicas

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top