iOS设置tableview的heightForHeaderInSection高度自适应

在开发的过程中,需要在viewForHeaderInSection的方法中创建视图,并且赋值,动态获取高度,但是在heightForHeaderInSection赋值的时候发现高度还是之前的。

原因是:视图执行的顺序是先

执行顺序

从上图可以看出,是先执行heightForHeaderInSection,再执行viewForHeaderInSection。也就是,先执行高度,再执行视图。
但是,为了高度能自适应视图内容的多少,需要在视图加载完成后,重新执行高度赋值的方法:
先执行heightForHeaderInSection
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {

       CGFloat hei = _headerHei > 0 ? _headerHei : 75;  
       NSLog(@"当前方法名: %@,当前分区:%ld",NSStringFromSelector(_cmd), (long)section);  
         return hei;

}

再执行viewForHeaderInSection
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

    self.HeaderView = [HuScoreRigHeaderV new];

    ContentListModel * listModel = self.rightArray[section];
 
    self.HeaderView.contentListM = listModel;
    //自适应高度:
    _headerHei = [self.HeaderView scoreRightCellHeight];  

    self.HeaderView.frame = CGRectMake(0, 0, HHBWIDTH / 3 * 2 - 10, _headerHei);

    NSLog(@"当前方法名: %@,当前分区 %ld",NSStringFromSelector(_cmd), (long)section);   

    return self.HeaderView ;  

}

解决方案:

在viewForHeaderInSection被调用后,让系统重新调用一次heightForHeaderInSection。因此,在viewDidAppear中让它重新调用一次。

 - (void)viewDidAppear:(BOOL)animated{

       [super viewDidAppear:animated];

       [self.rigTableView reloadData];

 }

最后的执行顺序为:

执行顺序

分割线

按照上面的写法,遇到了以下问题:

(1)通过cell所在的section获取sectionHeader的时候,视图为nil。之前sectionView继承UIView,在后面需要通过cell所在的section获取sectionHeader的时候,发现headerViewForSection:section方法获取的为nil。
点进去发现这个方法:

  - (nullable UITableViewHeaderFooterView *)headerViewForSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);
  - (nullable UITableViewHeaderFooterView *)footerViewForSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);

所以,在自定义sectionHeaderView的时候继承自UITableViewHeaderFooterView。

(2)在viewForHeader初始化方法错误:

    - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

    ContentListModel *listModel = self.rightArray[section];

    HuScoreRigHeaderV *headV = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"headv"];

    if (!headV) {

        headV = [[HuScoreRigHeaderV alloc] initWithReuseIdentifier:@"headv"];
    }

    headV.delegate = self;
    headV.contentListModel = listModel;

    return headV;

}

(3)注意plain和group对viewForHeader的影响。如果使用的是plain的话,会遇到当sectionHeader上的内容过多的时候,会发现sectionHeader遮挡住cell。主要是plain默认不是悬浮滑动的状态,它是相对gruop的sectionHeader来说是固定的,初始化的时候,样式改为group即可解决问题。

改进方法获取高度:

在开发的过程中,需要在 viewForHeaderInSection的方法中创建视图,并且赋值,动态获取高度,但是在heightForHeaderInSection赋值的时候发现高度还是之前的。

解决方案:

在数据请求回来的时候,计算自适应的高度,在model中新增“高度”的属性,将计算的高度直接赋值给model的这个属性。

数据解析的时候,直接在操作数据源获取高度:

             for (ItemListModel *itemListModel in leftArr) {
             for (int i = 0; i < itemListModel.itemContentList.count; i++) {
             ContentListModel *model = itemListModel.itemContentList[i];
                 //自适应高度:
             CGFloat contentHeight = [UIKitTool caculateHeight:model.content sizeOfWidth:kLableWidth - 30 withFont:[UIFont customFontWithName:@"PingFangSC-Regular" size:14.0]];
              model.height = 8 + contentHeight + 5     
  }

在heightForHeaderInSection的方法中直接区model中的属性即可。

       完整代码:

        //先执行高度:
      - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {

    ContentListModel *listModel = self.rightArray[section];

    CGFloat hei = listModel.height > 0 ? listModel.height : 75;

    return hei;

}

//再执行视图

     - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

    ContentListModel *listModel = self.rightArray[section];
    HuScoreRigHeaderV *headV = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"headv"];
    if (!headV) {
        headV = [[HuScoreRigHeaderV alloc] initWithReuseIdentifier:@"headv"];
    }
    headV.delegate = self;
    headV.contentListModel = listModel;
    return headV;

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 一、简介 <<UITableView(或简单地说,表视图)的一个实例是用于显示和编辑分层列出的信息的一种手段 <<...
    无邪8阅读 10,691评论 3 3
  • 概述在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,类似...
    liudhkk阅读 9,090评论 3 38
  • 废话不多说,直接上干货 ---------------------------------------------...
    小小赵纸农阅读 3,436评论 0 15
  • 1.创建一个UITableView对象 ITableView *tableView = [[UITableView...
    桐生一猫阅读 1,436评论 0 5
  • 【读经】 俄 【金句】 因你向兄弟雅各行强暴,羞愧必遮盖你,你也必永远断绝。(俄巴底亚书 1:10 和合本) 【感...
    chanor阅读 849评论 0 0