How to remove first cell top separator and last cell bottom separator

Anton Lovchikov picture Anton Lovchikov · Sep 19, 2015 · Viewed 8.6k times · Source

I need to remove top border form first cell in a particular section and bottom border form last cell in a particular section.

I googled solution for complete hiding separators from tableView, but I'd like to avoid it.

Also I've found solution when you get event when the cell will be displaying to play with separator insets. It works for middle separators, between two cells, but not for separator between header/footer and cell.

Plus I've tried to see sublayers in the header, but since this view is created by myself, I didn't found there separator view.

May be I should work with layers inside header? Please, give me a clue.

Answer

user2991638 picture user2991638 · May 18, 2017

Below code helps me to remove the separator from last row in a UITableView.

Swift 3.0

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        if indexPath.row == tableView.numberOfRows(inSection: indexPath.section) {
            cell.separatorInset.left = cell.bounds.size.width
        }
    }