简体   繁体   中英

how to change a particular header section height of uitableview dynamically in ios

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;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return headerSectionHeightDefault;
}

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

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];
}

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];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM