Home > Net >  How to fix spacing issues with headers and footers?
How to fix spacing issues with headers and footers?

Time:03-31

I have a header and a footer make in code. The problem I am having is that the footer of the current section impedes on the last cell in said section. Second, how do I add blank lines of space between the current sections footer and the next sections header?

Header code:

override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return myTitles[section]
    }

Footer code:

override func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
        return("Total Emojis: \(emojis[section].count)")
    }

CodePudding user response:

You can use a view for header and footer instead of titleForHeaderInSection and titleForFooterInSection .

For example:

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
            
                let view = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 18))
                let label = UILabel(frame: CGRect(x: view.centerX - 50, y: view.centerY, width: 100, height: 40))
                label.text = myTitles[section]
                label.textAlignment = .center
                view.addSubview(label)
                
                return view       
    }
    
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        
            return 60 
    }

And for footer you can use the same concept.

  • Related