美文网首页
iOS设置tableview的heightForHeaderIn

iOS设置tableview的heightForHeaderIn

作者: LibraGril | 来源:发表于2018-10-22 14:34 被阅读0次

    在开发的过程中,需要在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;
    

    }

    相关文章

      网友评论

          本文标题:iOS设置tableview的heightForHeaderIn

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