美文网首页tableview&&
iOS开发: 修改UITableView的tableHeader

iOS开发: 修改UITableView的tableHeader

作者: 伯wen | 来源:发表于2019-05-09 11:16 被阅读59次

    效果图

    修改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];
    }
    
    • 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;
    
    /** 页眉 */
    @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
    

    相关文章

      网友评论

        本文标题:iOS开发: 修改UITableView的tableHeader

        本文链接:https://www.haomeiwen.com/subject/fzaioqtx.html