Search code examples
iosobjective-ccelluitableview

Grouped UITableview remove outer separator line


I have a grouped UITableview which is created programatically. Also I have a cell with xib file populated in tableview programmatically as well. So far so good. But I want to only remove outer separator line. I used below code but this time removed all separator line.

self.tableView.separatorColor = [UIColor clearColor];

this is not good option for my situation. Here is the screenshot what i want to do;

enter image description here


Solution

  • I just worked out a solution, as the cell has contentView which is a UIView, so I think you can just focus on the bottomline of contentView.

    Here is my code:

    first, you have to make the separator to clear

    tableView.separatorColor = UIColor.clear
    

    Second, in the cellForRowAt function:

    let bottomBorder = CALayer()
    
    bottomBorder.frame = CGRect(x: 0.0, y: 43.0, width: cell.contentView.frame.size.width, height: 1.0)
    bottomBorder.backgroundColor = UIColor(white: 0.8, alpha: 1.0).cgColor
    cell.contentView.layer.addSublayer(bottomBorder)
    

    here you will see the UI like this:

    enter image description here