繁体   English   中英

如何在iOS中动态更改UITableView的特定标头部分的高度

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

我有一个UITableView,在标题部分有多个headerSection可用。 默认高度为56 现在,我想在每次单击特定部分的按钮时将特定的headerSection高度更改为40 单击将触发一个有助于更改高度的方法(sectionOpened:) 但是那时,另一个headerSection的高度应该保持为56 我怎么做? 到目前为止我的尝试:应该

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

通过使用此代码,您可以将部分的高度返回到40(即headerSectionHeightDefault),因此您需要为每个部分设置高度并分别返回它们。

我在相同的场景下工作并通过以下代码实现

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

而不是更新表视图,请在更改headerSectionHeightDefault的值后尝试重新加载表视图。

- (void) sectionOpened : (NSInteger) section
{
    if(section==0)
    {
        headerSectionHeightDefault=40;
    }
    else
    {
        headerSectionHeightDefault=56;
    }
    self.openSectionIndex = section;
    [menulistTable reloadData];
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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