Grouped UITableview remove outer separator line

前端 未结 19 2128
遇见更好的自我
遇见更好的自我 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:24

    iOS 13, Swift 5

    In the custom cell class:

    override func layoutSubviews(){
        super.layoutSubviews()
    
        for subview in subviews where (subview != contentView && abs(subview.frame.width - frame.width) <= 0.1 && subview.frame.height < 2) {
            subview.removeFromSuperview()                           //option #1 -- remove the line completely
            //subview.frame = subview.frame.insetBy(dx: 16, dy: 0)  //option #2 -- modify the length
        }
    }
    

    I want to thank @cook for this solution, as I built on top of it. I had some issues with their solution:

    1. it was removing the default highlight/selected background view, so I added an extra check on the subview's height.
    2. I put my code in layoutSubviews() and haven't had a single issue.
    3. I implemented an approximation between two CGFloats instead of using the equality operator ==, which sounds error-prone to me.

    I added the "option #2" in the code, as that's the solution I was personally looking for (I wanted to maintain the separator, but I wanted it to be at the same indentation level as the regular cell separators, in my case a value of 16).

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

    Here's my solution:

    self.tableView.separatorColor = self.tableView.backgroundColor
    

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

    Based on cook's answer, but without a timer:

    override func didAddSubview(_ subview: UIView) {
        super.didAddSubview(subview)
        if NSStringFromClass(subview.classForCoder) != "UITableViewCellContentView" && subview.frame.width == frame.width {
            subview.removeFromSuperview()
        }
    }
    

    Just add this in a cell's subclass and you don't need anything else. Works on iOS 12.

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

    iOS 10~13 only remove section head foot line.

    -(void)layoutSubviews{
        [super layoutSubviews];
        //for iOS10~iOS13: only remove section head foot line
        for (UIView * v in self.subviews) {
            if ( v != self.contentView &&
                (v.frame.size.width == self.frame.size.width)){
                [v removeFromSuperview];
            }
        }
    }
    

    if wan to remove all line:

        for (UIView * v in self.subviews) {
            if (v != self.contentView){
                [v removeFromSuperview];
            }
        }
    
    0 讨论(0)
  • 2020-12-02 11:32

    After inspecting the view hierarchy, it seems each UITableViewCell has only three subviews: the content view (UITableViewCellContentView), and two separator views (_UITableViewCellSeparatorView). I'm not a fan of dynamic class instantiation from NSStrings (and neither is Apple

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

    This is what I finally came up with:

    I was not able to find some better way than this. But it is working for me greatly. Last tried with Xcode Version 7.3.1 (7D1014). This procedure was done through storyboard.

    Basically I add a UIView of 0.5 pt Height on the UITableViewCell and then set a background color for that UIView. Set parent UITableView's Separator as None.

    Here is the details:

    Considering you already set your UITableViewCell, custom or default. On the very first stage set the UITableView's separator as None.

    Next add a UIView of 1 pt Height and set the Background as you need, on my case it is Red.

    Start setting the constraints. The problem is to set the height of the UIView as 0.5 pt. This is the only Problematic issue for this workflow.

    UIView with 0.5 pt Height:

    Sharing the way to set 0.5 pt height of the UIView.

    First(1) pin the view and then(2) set the height as 0.5. Press Enter.

    Finally your UIView will look similar like following.

    I was not able to set the height as 0.5 other than this way.

    0 讨论(0)
提交回复
热议问题