2014-12-26 46 views
0

我有一个UITableView,其中标题部分有多个headerSection可用。默认高度是。现在,只要我点击特定部分的按钮,我想将特定的headerSection高度更改为。点击触发方法(sectionOpened:),这有助于改变高度。但是那时候,其他headerSection的高度应该保持为。我怎么做?到目前为止,我尝试: 应该如何在ios中动态更改uitableview的特定标题部分高度

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

回答

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

通过使用此代码要退回部分的高度,以40(即headerSectionHeightDefault),所以你必须做什么设置高度为每个板块和归还个别。

+0

实现我已经做到了,但你的逻辑只适用我有一些静态的无节的说10.但如果我有动态数字那么你会做什么? – Poles

+0

在这种情况下,您必须管理要更改一个数组中高度的部分,并在返回部分高度时检查数组是否包含该特定部分,然后更改特定部分的高度否则返回默认高度 –

0

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

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

不起作用。同样的结果。 – Poles

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]; 
} 
相关问题