美文网首页
使用Masonry设置tableview自定义头视图、脚视图、区

使用Masonry设置tableview自定义头视图、脚视图、区

作者: 林希品 | 来源:发表于2022-07-27 14:55 被阅读0次

Masonry设置头视图

    HeaderView *headerView = [HeaderView new];
    tableView.tableHeaderView = tableView;
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(self.view);
    }];
    
    // 必须设置
    [headerView layoutIfNeeded];
    tableView.tableHeaderView = headerView;

使用系统方法设置头视图、脚视图自适应高度,脚视图不能使用上述头视图Masonry设置、否则脚视图的位置会从{0,0}开始

     HeaderView *headerView = [HeaderView new];
     CGSize size = [headerView    systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
     headerView.frame = CGRectMake(0, 0, size.width, size.height);
     tableView.tableHeaderView = headerView;

-------------------------------------------------------------------------------------------------

section区头、尾视图Masonry设置

    // 设置预设高度及自适应高度 cell设置同理
    tableView.estimatedSectionHeaderHeight = 10;
    tableView.sectionHeaderHeight = UITableViewAutomaticDimension;
    tableView.estimatedSectionFooterHeight = 10;
    tableView.sectionFooterHeight = UITableViewAutomaticDimension;

实现

// masonry布局就不需要返回高度
//- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
//
//}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view = [UIView new];
    
    UILabel *label = [UILabel new];
    label.text = @"主标题";
    [view addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self);
        make.top.mas_equalTo(10);
    }];
    
    UILabel *label2 = [UILabel new];
    label2.text = @"副标题";
    [view addSubview:label2];
    [label2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self);
        make.top.mas_equalTo(label.mas_bottom).mas_equalTo(10);
        make.bottom.mas_equalTo(self).mas_equalTo(-10);
    }];
    
    return view;
}

相关文章

网友评论

      本文标题:使用Masonry设置tableview自定义头视图、脚视图、区

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