![](/img/trans.png)
[英]How to animate the height change of an section header in 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.