Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 389
serhat sezer Avatar asked Mar 12 '15 09:03

serhat sezer


4 Answers

You can remove separators even in grouped UITableView with Static Cell:

class CustomCell: UITableViewCell {

override func layoutSubviews() {
    super.layoutSubviews()
    for view in subviews where view != contentView {
        view.removeFromSuperview()
    }
}
like image 117
morizotter Avatar answered Nov 03 '22 05:11

morizotter


Here's my solution:

self.tableView.separatorColor = self.tableView.backgroundColor

@xxtesaxx, you should have a real test

like image 22
Brook Avatar answered Nov 03 '22 05:11

Brook


Google, even in 2018, is serving this page as the top result for this question. I didn't have any luck in iOS 11 with any of the provided answers, so here's what I came up with:

extension UITableViewCell {
    func removeSectionSeparators() {
        for subview in subviews {
            if subview != contentView && subview.frame.width == frame.width {
                subview.removeFromSuperview()
            }
        }
    }
}

Calling .removeSectionSeparators() on any UITableViewCell instance will now take care of the problem. In my case at least, the section separators are the only ones with the same width as the cell itself (as the other ones are all indented).

The only question left is from where we should call it. You'd think willDisplayCell would be the best choice, but I discovered that the initial call occurs before the separator views themselves are generated, so no dice.

I ended up putting this in my cellForRowAtIndexPath method just before I return a reloaded cell:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "MyReusableIdentifier", for: indexPath)

    Timer.scheduledTimer(withTimeInterval: 0.15, repeats: false) { (timer) in
        cell.removeSectionSeparators()
    }

    return cell
}

It doesn't feel that elegant, but I haven't run into any issues yet.

EDIT: Looks like we need this too (for reused cells):

override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    cell.removeSectionSeparators()
}

Here's a before/after in screenshots with this code:

Before Before Separator Removal

After After Separator Removal

like image 20
cookednick Avatar answered Nov 03 '22 05:11

cookednick


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

like image 19
Qi Lee Avatar answered Nov 03 '22 03:11

Qi Lee