iosswiftuitableviewuitableviewsectionheader

Gap created UITableView where no section header or footer should be shown


I have a gap that is created between two sections and I don't know how to get rid of it.

I have 4 sections:

Section 1, 2, and 3 all have (same type) headers, but section 4 doesn't. Section 1, 2, and 4 have footers, but 3 doesn't.

On app load, there is a gap created between section 3 and 4 where there is no header & footer. I've tried to prevent this through the following code:

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let headerIdentifier = HomeViewSectionHeaderView.reuseIdentifier
        guard let view = tableView.dequeueReusableHeaderFooterView(
                withIdentifier: headerIdentifier)
                as? HomeViewSectionHeaderView
        else {
            return nil
        }
        if !sections[section].showSectionHeader {
            return nil
        }
        
        view.textLabel?.text = sections[section].title
        
        
        return view
    }
    
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        guard let view = tableView.dequeueReusableHeaderFooterView(
                withIdentifier: HomeViewSectionFooterView.reuseIdentifier)
                as? HomeViewSectionFooterView
        else {
            return nil
        }
        if !sections[section].showSectionFooter {
            return nil
        }
        
        return view
    }
    
    
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        if (sections[section].showSectionHeader){
            return 50.0
        }
        return 0.0
    }
    
    func tableView(_ tableView: UITableView, estimatedHeightForHeaderInSection section: Int) -> CGFloat {
        if (sections[section].showSectionHeader){
            return 50.0
        }
        return 0.0
    }
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        if (sections[section].showSectionFooter){
            return 8.0
        }
        return 0.0
    }
    
    func tableView(_ tableView: UITableView, estimatedHeightForFooterInSection section: Int) -> CGFloat {
        if (sections[section].showSectionFooter){
            return 8.0
        }
        return 0.0
    }

On the view hierarchy we can clearly see it is a gap without any elements: View hierarchy

The full screen looks like this: Full screen capture


Solution

  • Instead of returning 0 to heightForFooterInSection and heightForHeaderInSection try returning a non zero value (close to 0 of course) like 0.001 or .leastNormalMagnitude or .leastNonzeroMagnitude

    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
            if (sections[section].showSectionFooter){
                return 8.0
            }
            return .leastNormalMagnitude //or you can use .leastNonzeroMagnitude or return a non zero value like 0.001
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
            if (sections[section].showSectionHeader){
                return 50.0
            }
            return .leastNormalMagnitude //or you can use .leastNonzeroMagnitude or return a non zero value like 0.001
    }