美文网首页
使用tableView的HeaderView与footerVie

使用tableView的HeaderView与footerVie

作者: 小驴拉磨 | 来源:发表于2018-05-14 11:26 被阅读19次

    今天使用tableView是遇到一个问题,如图


    WX20180514-112539.png

    这个tableView中使用了footerview和headerView代理方法中我是这么写的

    //返回组头view
    - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
    {
        TCPersonalAboutModel *aboutModel = self.dataSourceArray[section];
        if (aboutModel.sublist.count <= 0) {
            return nil;
        }
        TCPersonalAboutHeaderView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:personalAboutHeaderCell];
        if (!headerView) {
            headerView = [[TCPersonalAboutHeaderView alloc] initWithReuseIdentifier:personalAboutHeaderCell];
        }
        return headerView;
    }
    
    - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
    {
        TCPersonalAboutModel *aboutModel = self.dataSourceArray[section];
        if (aboutModel.sublist.count <= 0) {
            return 0;
        }
        return 50;
    }
    
    //返回组头尾view
    - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
    {
        TCPersonalAboutModel *aboutModel = self.dataSourceArray[section];
        if (aboutModel.sublist.count <= 0) {
            return nil;
        }
    
        TCPersonalAboutFooterView *footerview = [tableView dequeueReusableHeaderFooterViewWithIdentifier:personalAboutFooterCell];
        if (!footerview) {
            footerview = [[TCPersonalAboutFooterView alloc] initWithReuseIdentifier:personalAboutFooterCell];
        }
        return footerview;
    }
    
    - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
    {
        TCPersonalAboutModel *aboutModel = self.dataSourceArray[section];
        
       if (aboutModel.sublist.count <= 0) {
                return 0;
        }
        
        return 60;
    }
    

    出现上边的原因是因为我返回了nil,高度最好也不要设为0;
    改正后的代码

    //返回组头view
    - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
    {
        TCPersonalAboutModel *aboutModel = self.dataSourceArray[section];
        if (aboutModel.sublist.count <= 0) {
            return [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGFLOAT_MIN, CGFLOAT_MIN)];
        }
        TCPersonalAboutHeaderView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:personalAboutHeaderCell];
        if (!headerView) {
            headerView = [[TCPersonalAboutHeaderView alloc] initWithReuseIdentifier:personalAboutHeaderCell];
        }
        headerView.dataModel = aboutModel;
        return headerView;
    }
    
    - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
    {
        TCPersonalAboutModel *aboutModel = self.dataSourceArray[section];
        if (aboutModel.sublist.count <= 0) {
            return CGFLOAT_MIN;
        }
        return 50;
    }
    
    //返回组头尾view
    - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
    {
        TCPersonalAboutModel *aboutModel = self.dataSourceArray[section];
        if (aboutModel.sublist.count <= 0) {
            return [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGFLOAT_MIN, CGFLOAT_MIN)];
        }
    
        TCPersonalAboutFooterView *footerview = [tableView dequeueReusableHeaderFooterViewWithIdentifier:personalAboutFooterCell];
        if (!footerview) {
            footerview = [[TCPersonalAboutFooterView alloc] initWithReuseIdentifier:personalAboutFooterCell];
        }
        return footerview;
    }
    
    - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
    {
        TCPersonalAboutModel *aboutModel = self.dataSourceArray[section];
        
        if (aboutModel.sublist.count <= 2) {
            if (aboutModel.sublist.count <= 0) {
                return CGFLOAT_MIN;
            }
            return 10;
        }
        return 60;
    }
    
    

    感谢坐我旁边的的小二货。。。。

    相关文章

      网友评论

          本文标题:使用tableView的HeaderView与footerVie

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