Space Between Sections in UITableview

前端 未结 11 2159
醉酒成梦
醉酒成梦 2020-12-08 12:37

I need to reduce the space between two sections ofUITableView. I looked at this question but the solution doesn\'t allow for my custom header view because it c

11条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-08 13:31

    For Swift 5+:

    There is some space for the headers and footers by default. That's why I was having the problem of setting an exact separation for the sections.

    My solution to have a separation between 2 sections is the following:

    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    
            if section == 0 {
                 return 24
            } else if section == 1 {
                 return 32
            }
        }
    
        override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
            nil
        }
    
        override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
            nil
        }
    
        override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
            CGFloat.leastNormalMagnitude
        }
    

    As you see for viewForFooterInSection and viewForHeaderInSection I needed to return nil.

    In case you only want to change the footerHeight, just return CGFloat.leastNormalMagnitude for heightForHeaderInSection, and return the heights for each section in heightForFooterInSection.

提交回复
热议问题