Ios – How to add separators between UITableView sections

cocoa-touchiosswiftuikituitableview

I'm implementing a tableview, which has 3 sections(section count remain static) and each section contain several rows.

Behaviour 1: Add section header to only the third section.

How did I achieve:

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    if section == 2 {
       //create and return header view
    }
    return UIView(frame: CGRect(x: 0, y:0, width: tableView.width, height: UIScreen.main.scale))
}

Behaviour 2: TableViewCells shouldn't have separator. But, Sections should be separated with a thin line. (similar to tableviewcell separator)

What did I try:

  • Add a one pixel height, tableView width UIView, with all required properties set(color, etc) and add it as a sub-view to the section header view's
  • Add drop shadow with CGPoint(0,1) to the section header views

None of them worked out.

How did I achieve this:

  1. Find last cell in every section,
  2. Create an UIView with 1px height, tableView's width and add it as a subview to the last cell

Though this seem to work. I feel, there must be a better way of doing this, rather than adding line to bottom of every section's last tableView cell. Does anyone know a better way of implementing such behaviour?

Best Answer

Set the Style of the Table View to Grouped: enter image description here