Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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(Lifelist),
  • 2(Dawn chorus), and
  • 3(Search)
  • 4(Birds)

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading