iOS開(kāi)發(fā): 修改UITableView的tableHeaderView高度

效果圖

修改UITableView的tableHeaderView高度

代碼

  • 核心代碼
- (void)downBtnClick:(UIButton *)sender
{
    // 獲取改變后headerView的高度
    CGFloat height = self.headerView.frame.size.height == 200 ? 400 : 200;
    
    // 獲取改變后headerView的frame
    CGRect frame = self.headerView.frame;
    frame.size.height = height;
    
    // 使用動(dòng)畫(huà)修改headerView高度
    [self.tableView beginUpdates];
    [UIView animateWithDuration:0.3 animations:^{
        self.headerView.frame = frame;
    }];
    [self.tableView endUpdates];
}
  • ViewController中完整代碼
#import "ViewController.h"

#define kWidth [UIScreen mainScreen].bounds.size.width
#define kHeight [UIScreen mainScreen].bounds.size.height

@interface ViewController () <UITableViewDataSource>

/** 表格視圖 */
@property (nonatomic, strong) UITableView *tableView;

/** 頁(yè)眉 */
@property (nonatomic, strong) UIView *headerView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"修改header高度";
    
    [self.view addSubview:self.tableView];
    self.tableView.tableHeaderView = self.headerView;
    
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:(UIBarButtonSystemItemDone) target:self action:@selector(downBtnClick:)];
    self.navigationItem.rightBarButtonItem = item;
}

#pragma mark - < 點(diǎn)擊事件 >

- (void)downBtnClick:(UIButton *)sender
{
    // 獲取改變后headerView的高度
    CGFloat height = self.headerView.frame.size.height == 200 ? 400 : 200;
    
    // 獲取改變后headerView的frame
    CGRect frame = self.headerView.frame;
    frame.size.height = height;
    
    // 使用動(dòng)畫(huà)修改headerView高度
    [self.tableView beginUpdates];
    [UIView animateWithDuration:0.3 animations:^{
        self.headerView.frame = frame;
    }];
    [self.tableView endUpdates];
}

#pragma mark - < 懶加載 >

- (UITableView *)tableView
{
    if (!_tableView) {
        self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight)];
        _tableView.dataSource = self;
    }
    return _tableView;
}

- (UIView *)headerView
{
    if (!_headerView) {
        self.headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kWidth, 200)];
        _headerView.backgroundColor = [UIColor redColor];
    }
    return _headerView;
}

#pragma mark - < table view data source >

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 20;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellId"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"cellId"];
    }
    cell.textLabel.text = [NSString stringWithFormat:@"%zd", indexPath.row];
    return cell;
}

@end
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容