ma11hew28
ma11hew28

Reputation: 126327

How to add space between UITableView sections?

How does iOS edit contact UITableView add space between sections while still showing previous sections' last cells' separators?

In the screenshot below, the first cell of the third section is highlighted. How do I add that white space above it without hiding the separator underneath the "add phone" cell above it? I tried adding a white section header, but that hides the separator of the cell above it.

enter image description here

Upvotes: 1

Views: 7637

Answers (3)

rockdaswift
rockdaswift

Reputation: 9983

On iOS 15 you can add padding to the section header

if #available(iOS 15.0, *) {
    tableView.sectionHeaderTopPadding = 0
}

Upvotes: 3

Alok
Alok

Reputation: 25938

If your concern is about only for spacing then try below code:

override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    return 40
}

Upvotes: 5

ma11hew28
ma11hew28

Reputation: 126327

As a hack, I just added a separator to the header view.

// MARK: UITableViewDelegate

override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return section == 0 ? UITableViewAutomaticDimension : 24
}

override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    if section == 0 {
        return nil
    } else {
        let view = UIView(frame: CGRectZero)
        view.backgroundColor = UIColor.whiteColor()
        let separator = UIView(frame: CGRect(x: tableView.separatorInset.left, y: 0, width: 0, height: 0.5))
        separator.autoresizingMask = .FlexibleWidth
        separator.backgroundColor = tableView.separatorColor
        view.addSubview(separator)
        return view
    }
}

Please answer if you have a clean & simple solution.

Upvotes: 5

Related Questions