Grouped UITableview remove outer separator line

前端 未结 19 2129
遇见更好的自我
遇见更好的自我 2020-12-02 10:58

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 t

相关标签:
19条回答
  • 2020-12-02 11:35

    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:

    0 讨论(0)
  • 2020-12-02 11:35

    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

    After

    0 讨论(0)
  • 2020-12-02 11:35

    Tried various solutions based on the cell.separatorInset = UIEdgeInsetsMake() workaround, none of them working properly.

    For my iOS11 UITableViewStyleGrouped based project, this did it:

    self.tableView.separatorColor = self.tableView.backgroundColor;
    
    0 讨论(0)
  • 2020-12-02 11:36

    For a single-cell section, simply overriding layoutSubviews and leaving it empty does the trick! https://stackoverflow.com/a/59733571/4442627

    0 讨论(0)
  • 2020-12-02 11:37

    You can access the view using

    override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    
        let header = view as! UITableViewHeaderFooterView 
        header.backgroundView . .....
    
    0 讨论(0)
  • 2020-12-02 11:42
     override func layoutSubviews() {
        super.layoutSubviews()
        subviews.filter { $0 != contentView && $0.frame.width == frame.width }.first?.removeFromSuperview()
    }
    
    0 讨论(0)
提交回复
热议问题