0

我有一个 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;
}
4

3 回答 3

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

通过使用此代码,您将部分的高度返回到 40(即 headerSectionHeightDefault),因此您必须为每个部分设置高度并单独返回它们。

于 2014-12-26T07:03:57.307 回答
1

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

- (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];
}
于 2015-11-19T07:49:59.767 回答
0

在更改 headerSectionHeightDefault 的值后,尝试重新加载 tableview 而不是 tableview 更新。

- (void) sectionOpened : (NSInteger) section
{
    if(section==0)
    {
        headerSectionHeightDefault=40;
    }
    else
    {
        headerSectionHeightDefault=56;
    }
    self.openSectionIndex = section;
    [menulistTable reloadData];
}
于 2014-12-26T07:08:27.277 回答