1

I want to be able to have all of my separators in a table view to be inset by a certain amount. This table view needs a custom header for each section. See the following image for the desired insets:

enter image description here

My storyboard is set up so that I have a UIViewController which contains a container UIView, which in turn has an embed segue for a UITableView. My table view specifies a prototype cell, and I have set in the storyboard a Grouped style, Default separator, with Automatic inset.

On my Table View Cell in the storyboard, I have a Custom separator inset of 26 on the left.

In my table view controller, I have implemented the following two methods:

override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: "ItemHeader") as! TableSectionHeaderViewCell
    header.title.text = "My Custom Date Header"
    return header
}

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

In a separate file I have subclassed UITableViewHeaderFooterView to just set a font, text color, and some constraints.

Now if I set a custom separator inset on the storyboard at the table view level with 26 inset from the left, either From Cell Edges or From Automatic Insets, nothing changes with the separators that appear below each of my headers. Similarly, if I set these values programmatically in the viewDidLoad method of my table view controller, like so:

override func viewDidLoad() {
    super.viewDidLoad()

    tableView.rowHeight = UITableView.automaticDimension
    tableView.register(TableSectionHeaderViewCell.self, forHeaderFooterViewReuseIdentifier: "ItemHeader")

    tableView.separatorInset = UIEdgeInsets(top: 0, left: 26, bottom: 0, right: 0)
    tableView.separatorStyle = .singleLine
    tableView.separatorInsetReference = .fromCellEdges
}

Nothing changes with the separator insets, even using the .fromAutomaticInsets option for the separator inset reference. Here is what I am actually seeing:

enter image description here

It seems that the first and last cells in a section are actually not respecting the separator insets. I thought originally that this was an issue related to having a header for each grouped section, but the separator on the final cell with no header below it seems to indicate otherwise. How can I make it so that these separators all are inset by 26pts? Thank you for any insight you can provide.

Daniel Allen
  • 894
  • 1
  • 10
  • 23

1 Answers1

2

Check this thread then, I think it will help:

Grouped UITableview remove outer separator line

I can see that they removed the default separator (Using Clear Color) and put a custom view for the separator themselves.

The other option I can suggest is don’t use a group style Table view, use the plain one and handle it yourself.

Amr El-Sayed
  • 384
  • 2
  • 7
  • Thank you for the suggestion; I did come across this post already, and it looks like they're trying to remove the separators, which is not what I want. I want to be able to change the insets of these separators, and each thing I've tried until now doesn't work. Unfortunately they don't demonstrate how to manipulate those separators in that thread; just how to get rid of them. – Daniel Allen Jan 30 '20 at 14:31
  • I can see that It removed them and put themselves a custom view for the separator. The other option I can suggest is don’t use a group style Table view, use the plain one. – Amr El-Sayed Jan 30 '20 at 15:59
  • 1
    I am starting to explore a plain table view as an option. It seems inevitable that you have to add your own separators with either approach unfortunately with no option of customization. I am strongly against approaches that require you to know about the underlying view structure since that's subject to change, so at least making the separators invisible and using a custom separator is a better option. – Daniel Allen Jan 30 '20 at 16:12