0

I have an UITableView where in the header part there are multiple headerSection available. The default height is 56. Now I want to change a particular headerSection height to 40 whenever I click on the particular section's button. the click triggers a method(sectionOpened:) which helps to change the height. But at that time, the other headerSection's height should remain 56. How do I do that? My attempt so far: should

float headerSectionHeightDefault;

- (void)viewDidLoad
{
    [super viewDidLoad];
    headerSectionHeightDefault=56;
}

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


- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    headerView = [[UIView alloc] initWithFrame: CGRectMake(0.0f, 0.0f, 640.0f, 0.0f)];

    UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(15, 0, 300, 40)];
    img.image = [UIImage imageNamed:@"menu_btn7.png"];

    [headerView addSubview:img];

    return headerView;
}

- (void) sectionOpened : (NSInteger) section
{
    [menulistTable beginUpdates];

    if(section==0)
    {
        headerSectionHeightDefault=40;
    }
    else
    {
        headerSectionHeightDefault=56;
    }

    [menulistTable endUpdates];
    self.openSectionIndex = section;
}
Poles
  • 3,585
  • 9
  • 43
  • 91

3 Answers3

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

by using this code you are returning height of the section to 40(i.e headerSectionHeightDefault), so what you have to do set the height for each section and return them individually.

Pratik Shah
  • 563
  • 4
  • 14
  • I have done that but your logic works only I have some static no of section say 10. But what if I have dynamic numbers of section then what you will do? – Poles Dec 26 '14 at 07:25
  • in this case you have to manage the sections in which you are changing the height in one array and while returning height of section check whether the array contains that particular section then change height for particular section else return default height – Pratik Shah Dec 26 '14 at 09:29
1

I worked on same scenario and achieved by following code

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section==0)
        return headerHeight;
    if (section==1)
        return headerHeight1;
    if (section==2)
        return headerHeight2;
    if (section==3)
        return headerHeight3;
    if (section==4)
        return headerHeight4;
    if (section==5)
        return headerHeight5;
    if (section==6)
        return headerHeight6;
}

-(void)changeHeight {
    UIView *headerSectionView = (UIView *)[mainTable viewWithTag:currentSectionIndex+1];
    [mainTable beginUpdates];

    if (currentSectionIndex ==1)
        headerHeight= updatedValue;
    else if (currentSectionIndex==2)
        headerHeight1= updatedValue;
    else if (currentSectionIndex==3)
        headerHeight2= updatedValue;
    else if (currentSectionIndex==4)
        headerHeight3= updatedValue;
    else if (currentSectionIndex==5)
        headerHeight4= updatedValue;
    else if (currentSectionIndex==6)
        headerHeight5= updatedValue;

    [mainTable endUpdates];
}
Antti29
  • 2,953
  • 12
  • 34
  • 36
datha
  • 359
  • 2
  • 14
0

instead of tableview update, try reloading the tableview after you changed the value of headerSectionHeightDefault.

- (void) sectionOpened : (NSInteger) section
{
    if(section==0)
    {
        headerSectionHeightDefault=40;
    }
    else
    {
        headerSectionHeightDefault=56;
    }
    self.openSectionIndex = section;
    [menulistTable reloadData];
}
Keshav
  • 2,965
  • 3
  • 25
  • 30