效果图
修改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;
// 使用动画修改headerView高度
[self.tableView beginUpdates];
[UIView animateWithDuration:0.3 animations:^{
self.headerView.frame = frame;
}];
[self.tableView endUpdates];
}
#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;
/** 页眉 */
@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 - < 点击事件 >
- (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;
// 使用动画修改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
网友评论